简体   繁体   English

C编程:从文件读取并在屏幕上打印

[英]C Programming: Read from file and print on screen

I am trying to read a file test.txt via fscanf and store it in a array of struct. 我试图通过fscanf读取文件test.txt并将其存储在结构数组中。 I have posted the code that I have tried. 我已经发布了我尝试过的代码。 Looks like problem here is with load function 看起来这里的问题是加载功能

This is what I have in test.txt: 这是我在test.txt中所拥有的

205,11.20,John Snow
336,23.40,Winter is coming
220,34.20,You know nothing

load function uses loadinput function to read test.txt file into the “item” array and sets the target of the “NoPtr” to the number of Items read from the file (which in this case should be 3). 加载函数使用loadinput函数将test.txt文件读入“ item”数组,并将“ NoPtr”的目标设置为从文件读取的项数(在这种情况下应为3)。

After reading the file, I am also trying to print it on screen, but it won't work. 读取文件后,我也尝试在屏幕上打印该文件,但是它不起作用。 Nothing is displayed at all. 什么都没有显示。

This program compiles. 该程序进行编译。 What is it that I am doing wrong here? 我在这里做错了什么?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Item {
   double value;
   int unit;
   char name[50];
};

int load(struct Item* item, char Name[], int* NoPtr);
int loadinput(struct Item* item, FILE* data);
void display(struct Item item, int variableA);

int main(void) 
{
    struct Item FIN[3];
   int i, n;
   load(FIN, "test.txt", &n);
   for (i = 0; i < n; i++) 
   {
      display(FIN[i],0);
   }
return 0;
}


int load(struct Item* item, char Name[], int* NoPtr)
{
    struct Item ARR[3];
    int flagcheck;
    FILE* fl;
    fl =  fopen("Name[]", "r");
    while (fl)
    {
        flagcheck = loadinput(&ARR, fl);
        if (flagcheck < 0)
        {
            fclose(fl);
            break;
        }
        else
        {
            *NoPtr++;
        }
    fclose(fl);
    }
return 0;
}

int loadinput(struct Item* item, FILE* data)
{
    int ret = fscanf(data, "%d,%lf,", &item->unit, &item->value);
    if (ret != 2) {
            return -1;
    }
    fgets(item->name, sizeof item->name, data);
    item->name[strlen(item->name)-1] = '\0';
    return 0;
}

void display(struct Item item, int variableA)
{
printf("|%3d |%12.2lf| %20s |***\n", item.unit, item.value, item.name);
return;
}

see fix demo – BLUEPIXY 请参阅修复演示– BLUEPIXY

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Item {
    double value;
    int unit;
    char name[50];
};

int load(struct Item* item, char Name[], int* NoPtr);
int loadinput(struct Item* item, FILE* data);
void display(struct Item item, int variableA);

int main(void) {
    struct Item FIN[3];
    int i, n;
    load(FIN, "test.txt", &n);
    for (i = 0; i < n; i++) {
        display(FIN[i],0);
    }
    return 0;
}

int load(struct Item* item, char Name[], int* NoPtr){
    *NoPtr = 0;
    int flagcheck;
    FILE* fl;
    fl =  stdin;//fopen(Name, "r");//for ideone
    while (fl){
        flagcheck = loadinput(&item[*NoPtr], fl);
        if (flagcheck < 0){
            fclose(fl);
            break;
        } else {
            ++*NoPtr;
        }
    }
    return 0;
}

int loadinput(struct Item* item, FILE* data){
    int ret = fscanf(data, "%d,%lf,", &item->unit, &item->value);
    if (ret != 2) {
        return -1;
    }
    fgets(item->name, sizeof item->name, data);
    item->name[strlen(item->name)-1] = '\0';
    return 0;
}

void display(struct Item item, int variableA){
    printf("|%3d |%12.2lf| %20s |***\n", item.unit, item.value, item.name);
    return;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM