C编程——如何在文件中保存带空格的字符串并调用它们

C编程——如何在文件中保存带空格的字符串并调用它们,c,C,我在这个网站上看到了一些类似的问题,并试图遵循这些方法,但要么这些方法不能正常工作,要么我不能正确理解 我在将带空格的字符串保存到文件中并调用它们时遇到问题 #include<stdio.h> int main() { char a[200]; char b[200]; char c[200]; char bug[100]; int s; FILE *fp; printf("Press 1 to add\nPress 2 to show\n"

我在这个网站上看到了一些类似的问题,并试图遵循这些方法,但要么这些方法不能正常工作,要么我不能正确理解

我在将带空格的字符串保存到文件中并调用它们时遇到问题

#include<stdio.h>
int main()
{
   char a[200];
   char b[200];
   char c[200];
   char bug[100];
   int s;
   FILE *fp;
   printf("Press 1 to add\nPress 2 to show\n");
   scanf("%d",&s);
   if(s==1)
      goto level1;
   else
      goto level2;
   level1:
      gets(bug);
      printf("Name: ");
      gets(a);
      printf("Address: ");
      gets(b);
      printf("Comment: ");
      gets(c);
      fp=fopen("practice2.txt", "a+");
      fprintf(fp, "\n%s\t%s\t%s\t",a,b,c);
      fclose(fp);
      printf("\n1 for add\n2 for show\n");
      scanf("%d",&s);
      if(s==1)
         goto level1;
      else
         goto level2;
      level2:
         fp=fopen("practice2.txt", "r");
         if(fp==0)
            printf("\nEmpty\n");
         else
         {
            while(!feof(fp))
            {
               fscanf(fp, "\n%s\t%s\t%s\t",&a,&b,&c);
               printf("%s\n%s\n%s\n\n",a,b,c);
            }
         }
         fclose(fp);
         return 0;
}

然后我希望它能以这种方式显示结果

%s-说明符,允许您读取整个单词直到delimeter,不记得确切的delimeter列表,但“space”\n“\0”\t在其中是100%

您可以使用一个缓冲区,在该缓冲区中,您可以一直读取到“\n”,然后使用该缓冲区执行所有操作

int main() {
    const int max_size = 1000;
    char tmp_symbol;
    char arr[max_size];
    int arr_counter = 0;

    while (true /*some code */) {
        scanf("%c", &tmp_symbol);
        if (tmp_symbol != '\n' /* '\t' or other delims you need*/)
            arr[arr_counter++] = tmp_symbol;
        else break /* or something , like perform your line*/;
    }

    // here i have whole line in arr;
    return 0;
}
试试这个,但不是那么完美

char* read_line(char* destination, FILE* source) {
    char tmp_symb; int counter = 0;
     while (true) {
        fscanf(source ,"%c", &tmp_symb);
        if (tmp_symb != '\n' && tmp_symb != EOF)
            destination[counter++] = tmp_symb;
        else return destination;
     };
}
在使用%s的地方使用,并使一些数组(如char)成为临时数组[200]

这段代码应该适合你。初始扫描用于清空缓冲区。代码相当容易理解

您应该避免使用GET,因为不可能知道GET将使用多少个字符

我在这里详细回答了你的问题。


}

非常感谢您的帮助。但很抱歉,我不明白在哪里应用你的方法。。。。。你能在我的代码中加入你的方法吗?这与问题有关,但那些goto真的很难看。你用什么替代goto?thjis小代码可以在没有gotos的情况下编写,是的。但我发现它对我的近千行项目很有用。杰克:我建议你在养成太多的坏习惯之前,尽快开始学习,比如说用goto代替Loop。@paul谢谢你的建议。但是,你能详细说明一下为什么要养成坏习惯吗?它是否有一些绑定,或者工作不正常或者类似的东西?@jake:goto通常被认为是有害的,原因有很多,但主要是它打破了结构化编程的概念。有一些相当罕见的情况下,goto可能是合适的。但一般来说,尤其是作为初学者,你应该完全避免它,学习循环等不起作用的结构化编程。我一定做错了。请这是一个请求,你能在我的代码中添加你的方法并发送吗?请帮助您得到的错误是什么或文件的输入是什么?在这里分享截图@tushitjain。请帮助FflushStdin在大多数平台上导致未定义的行为,不应使用它-fflush应仅用于输出流。@PaulR感谢您的评论。我已在没有fflushstdin的情况下编辑了我的解决方案。您可以卸下负片1:@杰克现在检查编辑的代码:这将帮助你。
char* read_line(char* destination, FILE* source) {
    char tmp_symb; int counter = 0;
     while (true) {
        fscanf(source ,"%c", &tmp_symb);
        if (tmp_symb != '\n' && tmp_symb != EOF)
            destination[counter++] = tmp_symb;
        else return destination;
     };
}
    char input[200];
    printf("\n\tEnter the name \n");
    scanf("% [^\n]s" , &input);
    fgets(input, 200, stdin); 
#include<stdio.h>
int main()
{
char a[200];
char b[200];
char c[200];
char line[200];
char bug[100];
int s;
FILE *fp;
printf("Press 1 to add\nPress 2 to show\n");
scanf("%d",&s);
if(s==1)
  goto level1;
else
  goto level2;
level1:
gets(bug);
//scanf("% [^\n]s" , &a);
    printf("Name:");
  //fflush(stdin);
  scanf("% [^\n]s" , &a);
  fgets(a, 200, stdin);
  printf("Address: ");
  scanf("% [^\n]s" , &b);
  fgets(b, 200, stdin);

  printf("Comment: ");
  scanf("% [^\n]s" , &c);
  fgets(c, 200, stdin);

  fp=fopen("practice2.txt", "a+");

  fprintf(fp, "\n %s %s %s",a,b,c);
  fclose(fp);

  printf("\n1 for add\n2 for show\n");
  scanf("%d",&s);
  if(s==1)
     goto level1;
  else
     goto level2;

  level2:
     fp=fopen("practice2.txt", "r");
     if(fp==0)
        printf("\nEmpty\n");
     else
     {
        //while(!feof(fp))
        while(fgets(line , sizeof(line), fp))
        {
           //fscanf(fp, "\n %s \t %s \t %s \t",&a,&b,&c);
           printf("%s",line);
        }
     }
     fclose(fp);
     return 0;