简体   繁体   English

解析文件txt C

[英]Parsing file txt C

I guys i've this part of my code: 伙计们,我在代码的这一部分:

void token(){
FILE *pointer;
user record;
pointer = fopen("utente_da_file.txt","r+");
printf("OK");   
fscanf(pointer , "%s, %s, %s, %s, %s \n" , record.nome_utente , record.nome , record.cognome , record.data_di_nascita , record.data_di_iscrizione);
fclose(pointer);
printf("TEXT -> %s \n" , record.nome_utente);
}

This is utente_da_file.txt 这是utente_da_file.txt

cocco,ananas,banana,ciao,miao 

This is my output: 这是我的输出:

TEXT -> cocco,ananas,banana,ciao,miao 

I don't understand why. 我不明白为什么。 Greetings :) 问候 :)

This is due to the nature of %s parameter in scanf family: it consumes all characters up to the first white space character it encounters – or up to the end of input, whichever comes first ( scanf - OK, C++ documentation, but applies for C alike). 这是由于scanf系列中%s参数的性质所致:它消耗所有字符,直到遇到的第一个空格字符–或直到输入结束,以先到者为准( scanf -OK,C ++文档,但适用于C一样)。 As you do not have any whitespace in your file, the entire content is consumed at once, including the commas, before you can scan for them in your format string... 由于您的文件中没有空格,因此可以立即使用整个内容(包括逗号),然后再以格式字符串对其进行扫描...

You would get a hint for if you checked the return value of (f)scanf - it returns the number of variables filled, so you should have got 1 as return value. 如果您检查了(f)scanf的返回值,您将得到提示-它返回填充的变量数,因此您应该以1作为返回值。

Problem with (f)scanf family is that you cannot specify the delimiters for your strings to stop. (f)scanf系列的问题是,您无法指定分隔符来停止字符串。 So in your case, you will have to append white space in between the words of the file. 因此,在您的情况下,您将不得不在文件的单词之间添加空格。 But be aware that the comma will be part of the string then, if you append whitespace after them, you would have to append whitespace before so that your format string can consume them - this might make your file ugly, though, so you might prefer dropping it entirely then (but then drop them in the format string, too!). 但要注意,逗号将字符串的一部分,那么,如果你他们之后追加的空白,你就必须追加空格之前 ,使您的格式字符串可以使用它们-这可能使你的文件难看,虽然如此,你可能更喜欢然后将其完全删除(但也将它们放在格式字符串中!)。

Alternatively, you can read the entire line at once using fgets and then parse it using strtok . 另外,您可以使用fgets一次读取整行,然后使用strtok对其进行解析。 The whole procedure could look similar to the following piece of code: 整个过程可能类似于以下代码:

char buffer[256];
fgets(buffer, sizeof(buffer), pointer);
char const* delimiters = ", \t\n\r";
char* token = strtok(buffer, delimiters);
if(token)
{
    strncpy(record.nome_utente, token, sizeof(record.nome_utente));
    if((token = strtok(NULL, delimiters)))
    {
        strncpy(record.nome, token, sizeof(record.nome));
        // rest alike...
    }
}

for me the best solution is to write thr C code in this way (a space between 2 %s): 对我而言,最好的解决方案是以这种方式编写thr C代码(2%s之间的空格):

fscanf(pointer , "%s %s %s %s %s \n" , record.nome_utente , record.nome , record.cognome , record.data_di_nascita , record.data_di_iscrizione);

and write your text file in this way (a space between two records): 并以这种方式写入文本文件(两个记录之间的空格):

cocco ananas banana ciao miao

In this way I'm sure it works well. 通过这种方式,我确定它会很好地工作。 Ciao e buona fortuna. Ciao e buona fortuna。

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

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