简体   繁体   English

从文件 c 读取

[英]Read From file c

I'm working on a project of booking, so the idea of project when the program starts it should read a data from a file called databook and save them on the struct, and everytime i'm adding a book it should add to program the difficult i found the name has a space between name and nickname so i used scanf but the problem is scanf don't read full line i used scanf("%[^\n]s",) but it doesnt work the source code below to understund more我正在做一个预订项目,所以当程序启动时,项目的想法应该从一个名为 databook 的文件中读取数据并将它们保存在结构中,并且每次我添加一本书时,它应该添加到程序中很难我发现名字在名字和昵称之间有一个空格所以我使用了 scanf 但问题是 scanf 不阅读完整行我使用了scanf("%[^\n]s",)但它不起作用下面的源代码了解更多

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

#define MAX_ARRAY_SIZE 5

typedef struct Book
{
    char BookName[50];
    int BookISBN;
    int Borrowed;
    char BorrowerName[50];
    char Field[50];
}Book;

Book book[MAX_ARRAY_SIZE];

void ReadFile(char* fileName);

int main(int argc, char* argv[])
{
    char* fileName = "c1.txt";

    ReadFile(fileName);

    int i = 0;

    for (i = 0; i < MAX_ARRAY_SIZE; i++)
    {
        printf("Book Name is : %s\n", book[i].BookName);
        printf("Book ISBN is : %d\n", book[i].BookISBN);
        printf("Borrowed is : %d\n", book[i].Borrowed);
        printf("Borrower Name is : %s\n", book[i].BorrowerName);
        printf("Field is : %s\n", book[i].Field);
        printf("\n");
    }
    exit(EXIT_SUCCESS);
}

void ReadFile(char* fileName)
{
    FILE* filePtr = NULL;
    int  i = 0;

    if ((filePtr = fopen(fileName, "r")) == NULL)
    {
        printf("Error : Unable to open %s for reading\n");
        exit(EXIT_FAILURE);
    }
    while (fscanf(filePtr, "%s%d%d%s%s", &book[i].BookName, &book[i].BookISBN, &book[i].Borrowed,&book[i].BorrowerName,&book[i].Field) != EOF)
    {
        i++;
    }

    fclose(filePtr);
} 

for databook用于数据簿

Technique Informatique //BookName1
90023 //BookISBN1
1 //(OR O) - means 'Borrowed OR not
Adam Ridge //BorrowerName1 (None in case Not borrowed)
special//(field)
Data Structures //BookName1
23451 //BookISBN1
0 //(OR O) - means 'Borrowed OR not
None //BorrowerName1 (None in case Not borrowed)
Computer Science //(field)
E-commerce Blockchain //BookName1
14678 //BookISBN1
1 //(OR O) - means 'Borrowed OR not
Adam Ridge //BorrowerName1 (None in case Not borrowed)
Business //(field)

As already pointed out by people in comments, you should try to use fgets() for reading file and then sscanf for reading integers from the string.正如人们在评论中已经指出的那样,您应该尝试使用fgets()读取文件,然后使用sscanf从字符串中读取整数。 Here is an example:这是一个例子:

int ReadFile(char* fileName) {
    FILE* filePtr = NULL;
    int  i = 0;

    if ((filePtr = fopen(fileName, "r")) == NULL) {
        printf("Error : Unable to open %s for reading\n");
        exit(EXIT_FAILURE);
    }

    char input[100];
    char *result;
    while ((result = fgets(input, 100, filePtr)) != NULL) {
        // Book Name
        input[strlen(input) - 1] = '\0'; // Trim trailing \n
        strcpy(book[i].BookName, input);

        // Book ISBN
        result = fgets(input, 100, filePtr);
        if (result == NULL) break;
        sscanf(input, "%d", &book[i].BookISBN);

        // Book Borrowed
        result = fgets(input, 100, filePtr);
        if (result == NULL) break;
        sscanf(input, "%d", &book[i].Borrowed);

        // Book Borrower Name
        result = fgets(input, 100, filePtr);
        input[strlen(input) - 1] = '\0'; // Trim trailing \n
        if (result == NULL) break;
        strcpy(book[i].BorrowerName, input);

        // Book Field
        result = fgets(input, 100, filePtr);
        input[strlen(input) - 1] = '\0'; // Trim trailing \n
        if (result == NULL) break;
        strcpy(book[i].Field, input);

        i++;
    }

    fclose(filePtr);
    return i;
} 

When I run this code on your file, I'm able to see documents being printed:当我在您的文件上运行此代码时,我可以看到正在打印的文档:

c-posts : $ gcc booking.c 
c-posts : $ ./a.out 
Book Name is : Technique Informatique
Book ISBN is : 90023
Borrowed is : 1
Borrower Name is : Adam Ridge 
Field is : special

Book Name is : Data Structures
Book ISBN is : 23451
Borrowed is : 0
Borrower Name is : None 
Field is : Computer Science 

Book Name is : E-commerce Blockchain 
Book ISBN is : 14678
Borrowed is : 1
Borrower Name is : Adam Ridge
Field is : Business 

Well you should be using fgets for reading string, as other people have already said.好吧,正如其他人已经说过的那样,您应该使用 fgets 来读取字符串。

Nevertheless, and to answer you question, here is some advice that might help you:尽管如此,为了回答您的问题,这里有一些可能对您有所帮助的建议:

  1. Book structure contains char arrays (char []) not pointers (char *) , so reading into those variables does nor have the & symbol, eg:书本结构包含 char arrays (char [])而不是指针(char *) ,因此读入这些变量也没有&符号,例如:

    fscanf(filePtr, "%[^\n]%d\n%d\n%[^\n]%*c%[^\n]%*c", book[i].BookName...) != EOF)

  2. The correct format of the fscanf function for this case is:这种情况下 fscanf function 的正确格式是:

fscanf(filePtr, "%[^\n]%d\n%d\n%[^\n]%*c%[^\n]%*c", book[i].BookName, &book[i].BookISBN, &book[i].Borrowed, book[i].BorrowerName, book[i].Field) != EOF)

Where:在哪里:

a) %[^\n] -> reads a full line a) %[^\n] -> 读取整行

b) %d\n -> reads an int and an end of line b) %d\n -> 读取一个 int 和一个行尾

c) [^\n]%*c -> reads a full line and any extra chars. c) [^\n]%*c -> 读取整行和任何额外的字符。 Note, that if you use * in the format string, it suppresses assignment:请注意,如果您在格式字符串中使用 *,它会禁止赋值:

%*c = read 1 character, but don't assign it to any variable %*c = 读取 1 个字符,但不将其分配给任何变量

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

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