简体   繁体   English

从文本文件读取和打印输出

[英]Reading and printout from a text file

I have to make a program written in C that will read and print out some things in a text file. 我必须编写一个用C编写的程序,该程序将读取并打印出文本文件中的某些内容。

But 2 function of the program below wont work for some reason. 但是以下程序的2个功能由于某种原因无法正常工作。 Case 3 works fine. 情况3正常。 But the other 2 print nothing or just 1 name. 但是其他2个不打印任何内容或仅打印1个名称。 How is this possible and how can I fix this. 这怎么可能,我该如何解决。

I don't get any errors btw. 顺便说一句,我没有任何错误。

#include <stdio.h>

main()
{
  FILE *list;
  int number, menu, total;
  char name[20];

  list = fopen("C:\\PATH\\TO\\FILE", "r");

  printf("Menu: \n"\n);
  printf("[1] Positive total: \n");
  printf("[2] Negative total: \n");
  printf("[3] Total: \n");
  printf("[4] Quit \n\n");
  printf("Choose: 1,2,3 or 4:\n");
  scanf("%d", &menu);

 while (menu!=4)
 {
        fscanf(list, "%d%s%d", &number, name, &total);
        switch(menu)
        {
                    case 1: printf("Negative total \n");
                            while(!feof(list))
                            {
                                if (total<0)
                                {
                                      printf("-%6d%-15s%6d\n", number, name , total);
                                      fscanf(list, "%d%s%d", &number, name, &total);
                                }
                            }
                            break;

                    case 2: printf("Positive: \n");
                            while(!feof(list))
                            {
                                if (total>=0)
                                {
                                      printf("-%6d%15s%6d\n", number, name , total);
                                      fscanf(list, "%d%s%d", &number, name, &total);
                                }
                            }
                            break;

                    case 3: printf("Total: \n");
                            while(!feof(list))
                            {
                                printf("-%6d%15s%6d\n", number, name , total);
                                fscanf(list, "%d%s%d", &number, name, &total);
                            }
                            break;
        }

        rewind(list);
        printf("\nChoose again, 1, 2, 3, 4: ");
        scanf("%d", &menu);
        }
        printf("End of Program");
        fclose(list);

        return 0;
}

The text file consist of numbers, names, total money they have (you could see it as a bank system). 文本文件由数字,名称,总资产组成(您可以将其视为银行系统)。 Here is the text file: 这是文本文件:

5892 John +7250
5893 Kate -94
5894 Mike +324
5895 Jack +9570
5896 Anne +800

You have an infinite loop condition in both case 1 and 2. In case 1, this: 您在情况1和2中都具有无限循环条件。在情况1中,这是:

if (total<0)
{
    printf("-%6d%-15s%6d\n", number, name , total);
    fscanf(list, "%d%s%d", &number, name, &total);
}

Should, instead, be: 相反,应为:

if (total<0)
{
      printf("-%6d%15s%6d\n", number, name , total);
}
fscanf(list, "%d%s%d", &number, name, &total);

Otherwise, you will be stuck every time you read a positive amount of money: total will be >= 0, so you don't enter the if block, and you never perform the fscanf() call to keep on reading the file. 否则,您每次读取正数的钱都会被困住:总数将> = 0,因此您不会输入if块,并且从不执​​行fscanf()调用来继续读取文件。 The loop expects to reach the end of file, but you never reach it because you're not reading forward, you just stopped. 循环期望到达文件的末尾,但是您从未到达它,因为您没有向前读,只是停止了。

The same happens for case 2: you have to call scanf outside of the if block, like this: 对于情况2同样如此:您必须在if块之外调用scanf,如下所示:

if (total>=0)
{
      printf("-%6d%15s%6d\n", number, name , total);
}
fscanf(list, "%d%s%d", &number, name, &total);

Also, the code you posted doesn't compile on line 11 (you have printf("Menu: \\n"\\n), ie, with a newline out of the string. Please try to provide compilable code in the future. 另外,您发布的代码不会在第11行上编译(您具有printf(“ Menu:\\ n” \\ n),即字符串中有换行符。请在以后尝试提供可编译的代码。

Here's a working version: 这是一个工作版本:

#include <stdio.h>

int main()
{
  FILE *list;
  int number, menu, total;
  char name[20];

  list = fopen("example", "r");

  printf("Menu: \n\n");
  printf("[1] Positive total: \n");
  printf("[2] Negative total: \n");
  printf("[3] Total: \n");
  printf("[4] Quit \n\n");
  printf("Choose: 1,2,3 or 4:\n");
  scanf("%d", &menu);

 while (menu!=4)
 {
        fscanf(list, "%d%s%d", &number, name, &total);
        switch(menu)
        {
                    case 1: printf("Negative total \n");
                            while(!feof(list))
                            {
                                if (total<0)
                                {
                                      printf("-%6d%15s%6d\n", number, name , total);
                                }
                                fscanf(list, "%d%s%d", &number, name, &total);
                            }
                            break;

                    case 2: printf("Positive: \n");
                            while(!feof(list))
                            {
                                if (total>=0)
                                {
                                      printf("-%6d%15s%6d\n", number, name , total);
                                }
                                fscanf(list, "%d%s%d", &number, name, &total);
                            }
                            break;

                    case 3: printf("Total: \n");
                            while(!feof(list))
                            {
                                printf("-%6d%15s%6d\n", number, name , total);
                                fscanf(list, "%d%s%d", &number, name, &total);
                            }
                            break;
        }

        rewind(list);
        printf("\nChoose again, 1, 2, 3, 4: ");
        scanf("%d", &menu);
        }
        printf("End of Program");
        fclose(list);

        return 0;
}

Some final remarks: I would test for fopen()'s return value to make sure the file was successfully opened. 最后的几点评论:我将测试fopen()的返回值,以确保文件已成功打开。 You might also want to perform some error checking with ferror() on your loop conditions. 您可能还想在循环条件下使用ferror()执行一些错误检查。 Finally, make sure that your "name" buffer is big enough, 20 characters is not very large. 最后,确保您的“名称”缓冲区足够大,20个字符不是很大。 For a safe program, you should use fgets(name, 19, stdin) instead, and then null-terminate the string. 为了安全起见,应改用fgets(name,19,stdin),然后以空值终止该字符串。

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

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