簡體   English   中英

用C讀取和打印文件

[英]Reading and printing a file in C

好的,我正在嘗試讀取包含我的信息的文本文件。 文本文件的內容是這樣的:

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

#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);
}

輸出值

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

我要說的是廢話,這是程序的輸出。 唯一起作用的是元素,但這只是循環的計數,因此沒有問題。 另外,當我取消注釋文件結尾循環時,我的程序崩潰。 抱歉,我不知道如何編輯列表。 在此先感謝您的幫助。

編輯代碼

#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);
}

您可能會溢出緩沖區並破壞'\\0'終結符,請嘗試此操作

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

並且還要檢查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);
}

不要以為一切都會好起來的,還要檢查fopen()沒有失敗

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

這段代碼

#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;
}

不應該失敗。

合並@ihorob的解決方案后,我得到了一個有效的代碼。

非常感謝。

加成:

刪除#include <unistd.h> ,在此示例中它不是必需的,並引起懷疑是否與Windows OS相關。

這是代碼:

#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