0

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.

Paul R
  • 208,748
  • 37
  • 389
  • 560
jake
  • 19
  • 1
  • 6
  • 1
    Uurelated to the question, but those gotos are really ugly. – Jabberwocky Apr 07 '17 at 06:44
  • what alternative of goto do you use? thjis small code can be written without gotos, yes. but i find it useful for doing my project of nearly thousand lines. what do you use in those cases? – jake Apr 07 '17 at 10:30
  • @jake: I recommend you get yourself [a good book on C](http://stackoverflow.com/a/562377/253056) and study it ASAP, before you pick up too many bad habits (like using `goto` instead of loops). – Paul R Apr 07 '17 at 13:47
  • @paul thanks for your advice. but can you please elaborate why is goto a bad habit? does it have some bindings or doesn't it work properly or anything like that? – jake Apr 07 '17 at 13:59
  • @jake: `goto` is generally considered harmful for a variety of reasons, but mainly it breaks the concept of *structured programming*. There are some fairly rare cases where a `goto` might be appropriate. but in general, and particularly as a beginner, you should completely avoid it and learn structured programming with loops etc. – Paul R Apr 07 '17 at 14:27
  • @jake: also, on another point, see: [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/q/5431941/253056) – Paul R Apr 07 '17 at 14:30

3 Answers3

1
    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

Community
  • 1
  • 1
tushitjain
  • 26
  • 6
0

%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]

bobra
  • 615
  • 3
  • 18
  • Thank you very much for your help. But I am sorry to say that I can't understand where to apply your method..... Can you PLEASE PLEASE include your method in my code? – jake Apr 07 '17 at 06:23
0
#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;

}

tushitjain
  • 26
  • 6
  • `fflush(stdin)` results in undefined behaviour on most platforms and should not be used - `fflush` should only be used on **output** streams. – Paul R Apr 07 '17 at 13:43
  • @PaulR thank you for that comment. I have edited my solution without **fflush(stdin)** . You can remove the negative 1. :) – tushitjain Apr 07 '17 at 21:59
  • @jake check the edited code now :) This will help you. – tushitjain Apr 07 '17 at 22:00