简体   繁体   English

用C读取和打印文件

[英]Reading and printing a file in C

Ok, I'm trying to read in a text file with my information. 好的,我正在尝试读取包含我的信息的文本文件。 The text file contents is this: 文本文件的内容是这样的:

gasData.txt gasData.txt

0 987654 201200 4.000000
1 red 89114 0.000000
2 red 89712 13.500000
3 red 90229 15.300000
4 987654 201001 0.000000
5 987654 201111 5.200000
6 987654 201612 25.299999
7 red 89300 7.100000
8 green 16 0.000000
9 green 216 20.000000
10 green 518 61.000000
11 green 879 50.000000

CODE

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>

#define N 12

struct record
{
    char id[7];
    int odometer;
    float gallons;
};
typedef struct record record_t;

record_t gasData[N];

int main(int argc, char *argv[])
{

    FILE *fileInput;
    float gallons;
    int element;
    char id[10], odometer[10];

    fileInput = fopen("gasData.txt", "r");

    for(element = 0; element < N; element++)
    {
        fscanf(fileInput, "%s %s %f", id, odometer, &gallons);

        /*if (feof(fileInput))
        {
            printf("end-of-file detected on file in\n");
            exit(1);
        }*/

        printf("element = %d:, id = %s, odometer = %s, gallons = %f\n", element, id, odometer, gallons);
    }

    exit (0);
}

OUTPUT 输出值

element = 0:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 1:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 2:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 3:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 4:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 5:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 6:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 7:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 8:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 9:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 10:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 11:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000

What I'm getting out is nonsense, here is my output from the program. 我要说的是废话,这是程序的输出。 The only thing that works is the element, but that's just the count of the loop, so no problem there. 唯一起作用的是元素,但这只是循环的计数,因此没有问题。 Also, when I uncomment my end-of-file loop, my program crashes. 另外,当我取消注释文件结尾循环时,我的程序崩溃。 Sorry I don't know how to edit the list. 抱歉,我不知道如何编辑列表。 Thanks in advance for any help. 在此先感谢您的帮助。

EDITED CODE 编辑代码

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>

#define N 12

/*struct record
{
    char id[7];
    int odometer;
    float gallons;
};
typedef struct record record_t;

record_t gasData[N];*/

int main(int argc, char *argv[])
{

    FILE *fileInput;
    float gallons;
    int element;
    char id[10]; char odometer[10];

    fileInput = fopen("gasData.txt", "r");
    if (fileInput == NULL)
        return -1;

    for(element = 0; element < N; element++)
    {
        if (fscanf(fileInput, "%9s%9s%f", id, odometer, &gallons) == 3)
        {
            printf("element = %d:, id = %s, odometer = %s, gallons = %f\n",
                element, id, odometer, gallons);
        }
    }

    exit (0);
}

You are likely overflowing the buffers and overwrinting the '\\0' terminator try this 您可能会溢出缓冲区并破坏'\\0'终结符,请尝试此操作

fscanf(fileInput, "%9s%9s%f", id, odometer, &gallons);

and also, check that fscanf() did succeed, otherwise the values will be uninitialized and the same thing will happen so 并且还要检查fscanf()成功,否则值将未初始化并且会发生相同的情况,因此

if (fscanf(fileInput, "%9s%9s%f", id, odometer, &gallons) == 3)
{
    printf("element = %d:, id = %s, odometer = %s, gallons = %f\n", 
        element, id, odometer, gallons);
}

don't assume that things are going to go fine, also check that fopen() didn't fail 不要以为一切都会好起来的,还要检查fopen()没有失败

fileInput = fopen("gasData.txt", "r");
if (fileInput == NULL)
    return -1;

This code 这段代码

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>

#define N 12

struct record
{
    char id[7];
    int odometer;
    float gallons;
};
typedef struct record record_t;

record_t gasData[N];

int main(int argc, char *argv[])
{

    FILE *fileInput;
    float gallons;
    int   element;
    char  id[10];
    char  odometer[10];

    fileInput = fopen("gasData.txt", "r");
    if (fileInput == NULL)
    {
        fprintf(stderr, "cannot open `gasData.txt'\n");
        return -1;
    }
    for(element = 0; element < N; element++)
    {
        if (fscanf(fileInput, "%9s%9s%f", id, odometer, &gallons) == 3)
        {
            printf("element = %d:, id = %s, odometer = %s, gallons = %f\n",
                element, id, odometer, gallons);
        }
    }

    return 0;
}

should not fail. 不应该失败。

After incorporating @ihorob's solution, I got the a working code. 合并@ihorob的解决方案后,我得到了一个有效的代码。

Thanks a lot. 非常感谢。

Addition: 加成:

Remove the #include <unistd.h> , it is not required in this example and causes doubt in its relavence to Windows OS. 删除#include <unistd.h> ,在此示例中它不是必需的,并引起怀疑是否与Windows OS相关。

Here is the code: 这是代码:

#include <stdio.h>
#define N 12

struct record
{
    char id[7];
    int odometer;
    float gallons;
};
typedef struct record record_t;

record_t gasData[N];

int main(int argc, char *argv[])
{
    float gallons = 0.0;
    int element = 0;
    char id[10] = { 0 }, odometer[10] = { 0 };
    FILE *fileInput = fopen("gasData.txt", "r");
    if (fileInput == NULL)
        return -1;
    for (element = 0; element < N; element++)
        if (fscanf(fileInput, "%9s%9s%f", id, odometer, &gallons) == 3)
            printf("element = %d:, id = %s, odometer = %s, gallons = %f\n", element, id, odometer, gallons);

    return(0);
}

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

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