简体   繁体   English

C 编码 - 文本文件中的第一个数字未按我预期的方式读取

[英]C coding - First number in text file not being read the way I expected

I'm doing a function that consists of reading elements of a file I wrote and priting them.我正在做一个 function ,其中包括读取我编写的文件的元素并打印它们。 The problem is, it seems the first number for every line of the text file is being read and printed in a way I don't understand on how it arrived.问题是,似乎文本文件每一行的第一个数字正在以一种我不明白它是如何到达的方式被读取和打印。 It's mostly numbers from 90-110 and zeros.它主要是 90-110 和零的数字。

I tried chaging the variable to chars and floats, but neither worked.我尝试将变量更改为字符和浮点数,但都没有奏效。

void imprimir_tabela(){
FILE *tabela = fopen("tabela.txt", "r");

if(tabela == NULL){
    printf("TABELA INVALIDA OU NAO ACHADA");
    printf("erro 404");
    exit(404);
}
int numero_atomico;
char abreviacao;
char nome[20];
float massa_atomica;
int grupo;
int periodo;

while(!feof(tabela))
{
fscanf(tabela, "%d %s %s %f %d %d", &numero_atomico, &abreviacao, &nome, &massa_atomica, &grupo, &periodo);
printf("%d - %s - %s - Massa atomica: %0.3f - Grupo: %d - Periodo: %d\n", numero_atomico, &abreviacao, &nome, massa_atomica, grupo, periodo);
}
rewind(tabela);
return 0;

Sample lines of the text file文本文件的示例行

1 H Hidrogenio 1.008 1 1
2 He Helio 4.003 18 1
3 Li Litio 6.941 1 2

The code, the result and the text file代码、结果和文本文件

See comments in the code below请参阅下面代码中的注释

void imprimir_tabela(){
FILE *tabela = fopen("tabela.txt", "r");

if(tabela == NULL){
    printf("TABELA INVALIDA OU NAO ACHADA");
    printf("erro 404");
    exit(404);
}
int numero_atomico;
char abreviacao;
char nome[20];
float massa_atomica;
int grupo;
int periodo;

do { // Use to be while(!feof(tabela)) - See link in the comments section
{
// 1 H Hidrogenio 1.008 1 1 
// For reference
// 1. Prevent buffer over run
// 3. record return value

int ret = fscanf(tabela, "%d %c %19s %f %d %d\n", &numero_atomico, &abreviacao, &nome, &massa_atomica, &grupo, &periodo);

switch (ret) {
  case 6: // Ok 
     printf("%d - %c - %s - Massa atomica: %0.3f - Grupo: %d - Periodo: %d\n", numero_atomico, abreviacao, &nome, massa_atomica, grupo, periodo);
     break;
  case EOF: // End of file - we are done
     break:
  default:
     // Report error - take some action - in this case exit
     fprintf(stderr, "Error in file %s\n", "tabela.txt");
     exit(-1);
// Are we done    
} while (ret != EOF);

// rewind(tabela); - Not required
// But this is
fclose(tabela);
return 0;
}

And please fix the indentation - I leave that as an exercise to the reader请修正缩进 - 我把它作为练习留给读者

At least this problem: reading into char as if it was enough for a string .至少这个问题:读入char就好像它对于string来说已经足够了。

char abreviacao;

//                    vv --- needs more meory than 1 `char`
fscanf(tabela, "%d %s %s %f %d %d", 
    &numero_atomico, &abreviacao, &nome, &massa_atomica, &grupo, &periodo);

Same problem with printf() printf()有同样的问题


Also

  • Limit input width of string限制字符串的输入宽度

  • check the return value of scanf() .检查scanf()的返回值。


int numero_atomico;
//char abreviacao;
char abreviacao[4];  // Some elements need 3 letters.
char nome[20];
float massa_atomica;
int grupo;
int periodo;

// while(!feof(tabela))
while(fscanf(tabela, "%d %3s %19s %f %d %d", 
    &numero_atomico, abreviacao, nome, &massa_atomica, &grupo, &periodo) == 6) {
  printf("%d - %s - %s - Massa atomica: %0.3f - Grupo: %d - Periodo: %d\n", 
      numero_atomico, &abreviacao, &nome, massa_atomica, grupo, periodo);
}
// rewind(tabela); // not needed

Recommend also to use double massa_atomica .还推荐使用double massa_atomica

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

相关问题 如何读取文件的第一个数字? - How can I read the first number of a file? 有没有办法找到从文件中读取的当前行的行号? - Is there a way to find the line number of the current line being read from a file? 我如何使该文件的每一行的第一个数字递增? 那第一行不被跳过呢? 在C中 - How do I get this file the first number of each line to increment it? And what about the first line not being skipped? In C 一旦读取文本文件,是否可以从文本文件中删除第一个字符? - Is there a way to delete the first character from a text file once it is read? 如果您知道字符编码,如何从C中的文本文件读取,然后在控制台上显示它? - How would you read from a text file in C if you know the character coding, then display it on the console? 从读取文件中修改第一个数字 c 语言 - modify first number from a read file c language 读取文本文件和C中每个单词的大写首字母 - Read a text file and uppercase first letter of every word in C 我将如何读取 C 中的文本文件? - How would I read a text file in C? C:从文件中读取 int 值只会导致读取文件的第一行 - C: Reading int values from a file only results in first line of the file being read 从C中的文本文件读取解析数据的最佳方法? - Best way to read a parse data from text file in C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM