簡體   English   中英

C 編程——如何在 FILE 中保存帶空格的字符串並調用它們

[英]C programming— how to save a string with spaces in FILE and recall them

我在這個網站上看到的類似問題很少,並嘗試遵循這些方法,但這些方法無法正常工作或我無法正確理解。

我無法將帶空格的字符串保存到文件中並調用它們。

#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;
}

如果我輸入沒有空格的字符串(如“Hello_people”),則此代碼有效。 但是,如果我輸入帶有“Hello people”之類的空格的字符串,則它不會顯示正確的輸出。 我正在做一個 c 編程項目,但遇到了這個問題。

順便說一句,在第 17 行,我使用了 get(bug)。 因為我發現如果我在 scanf() 之后使用 gets(),那么它不會接受第一個 gets() 輸入。 所以我嘗試在 scanf() 之后使用額外的 gets() ,然后它就起作用了。

我是編程的初學者。 請任何人都可以通過修復我的代碼以使其完美運行來幫助我嗎?


附加信息:

在此處輸入圖片說明

如果我輸入:

 Name: Shane Watson Address: Australia Comment: I like him

然后我希望它以這種方式顯示結果。

    char input[200];
    printf("\n\tEnter the name \n");
    scanf("% [^\n]s" , &input);
    fgets(input, 200, stdin); 

這段代碼應該適合你。 初始 scanf 用於清空緩沖區。 代碼相當簡單易懂。

您應該避免使用gets,因為無法知道gets()將使用多少個字符。

我已經在這里詳細回答了你的問題。 https://stackoverflow.com/a/43279647/7829296

%s - 說明符,允許讀取整個單詞直到 delimeter ,不記得確切的 delimeter list ,但是'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 temporary[200]數組char temporary[200]

#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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM