jake
jake

Reputation: 19

C programming--- how to save a string with spaces in FILE and recall them

I saw few similar problems in this website and tried to follow those methods but either those methods don't work properly or I couldn't understand properly.

I am having trouble with saving strings with spaces into file and recalling 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;
}

This code works if I input strings with no spaces like "Hello_people". But if I input strings with spaces like "Hello people", then it doesn't show proper output. I am working on a project in c programming and got stuck with this problem.

Btw, in line number 17, I used gets(bug). Because I found out that if i use gets() after scanf(), then it doesn't take the first gets() input. So I tried using an extra gets() after scanf() and then it works.

I am a very beginner in programming. Please can anyone help me by fixing my code to make it work perfectly?


Additional info:

enter image description here

If I input:

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

Then I expect it to show the result exactly this way.

Upvotes: 0

Views: 3968

Answers (3)

tushitjain
tushitjain

Reputation: 36

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

}

Upvotes: 0

tushitjain
tushitjain

Reputation: 36

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

This code should work for you. The initial scanf is used to empty the buffer. The code is fairly simple to understand.

You should avoid using gets as it is impossible to know that how many characters gets() will use.

I have given detailed answer to your question here. https://stackoverflow.com/a/43279647/7829296

Upvotes: 1

bobra
bobra

Reputation: 625

%s - specifier, allow to read you whole word till delimeter , dont remember exact delimeter list , but 'space' '\n' '\0' '\t' are 100% in there

you may use a buffer where you read until '\n' and then do everything you want with it.

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

try this, but its not that perfect

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

use in places where you use "%s", and make some array like char temporary[200]

Upvotes: 0

Related Questions