Save string with spaces to file

This is a discussion on Save string with spaces to file within the C Programming forums, part of the General Programming Boards category; I'm having a problem with this. I input some values with spaces and the compiler when I put a space ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    19

    Save string with spaces to file

    I'm having a problem with this.
    I input some values with spaces and the compiler when I put a space in a string start to input that information to the next field and when i list those items they are all wrong...but in the File they are in the correct order.

    For example, I input:

    Name: John Doe
    ID: 123
    Age: 50

    In the txt file will appear
    "
    John Doe
    123
    50"

    But when i got to list the employees it will appear:
    Name: John
    ID: Doe
    Age: 123

    I have this function:

    Code:
    addEmp{
          int i=0;
          loadEmployee();  
    
    do{
         
          printf("\n\nName:");
          scanf(" ");
          gets(employee[total_employees].name);
          printf("\nID:");
          scanf(" ");
          gets(employee[total_employees].id);
          printf("\nAge:");
          scanf(" ");
          gets(employee[total_employees].age);
    
    total_employees++;
    saveEmployee(); 
    printf("\nrepeat?");
    scanf(" %c", &repeat);
    }while(repeat == 's')
          
    }
    The functions to load and save are:

    Code:
    loadEmployee(){  
                             
         int i=0;
         
          file = fopen("emp.txt", "r");
          
          if(file==NULL)
          file = fopen("emp.txt", "w");
         
          
          else{
                   fscanf(file," %i",&total_employees);
                   for(i=0;i<total_employees;i++){
                   fscanf(file," %s", employee[i].name);
                   fscanf(file," %s", employee[i].id);
                   fscanf(file," %s", employee[i].age);
                
                   }
    }
          fclose(file);
    }
    
    saveEmployee(){
           
           int i=0;
                     
          file = fopen("emp.txt", "w");
          fprintf(file," %i\n",total_employees);
          
               for(i=0;i<total_employees;i++){
               fprintf(file,"%s\n", employee[i].name);
               fprintf(file,"%s\n", employee[i].id);
               fprintf(file,"%s\n", employee[i].age);
              
               }
               fclose(file);
          }
    Any help?
    Last edited by Gotze; 01-24-2012 at 02:19 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If this is homework and you're being taught to program this way, get a new teacher.

    If this is for "work", well, I promised myself that from now on if I have nothing nice to say I won't say anything ... so I have nothing to say.

    The first block of code you posted is not even a function.

    You shouldn't be relying on global variables the way you are.

    You should have an explicit return type and arguments (or void) for your functions.

    The problem you're having is caused by using the %s format specifier in fscanf in loadEmployee, which stops reading upon hitting a space. Try %[^\n] if your fields are separated by newlines.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com
    Code:
    do{
          
          printf("\n\nName:");
          scanf(" ");
          gets(employee[total_employees].name);
          printf("\nID:");
          scanf(" ");
          gets(employee[total_employees].id);
          printf("\nAge:");
          scanf(" ");
          gets(employee[total_employees].age);
    Why are those lines there?




    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    19
    That trick worked, thank you very much.
    This is not a "homework", i'm doing this project since the 12th of december, it was 99% done, now it's 100%.

    The example that you saw it was an example that i inventend right now just for you to understand what I mean, of course that the original code is much more complex(about 3000 lines) and the printf's and the variables of my code are not in english.

    Thank you again for this little trick.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    19
    Quote Originally Posted by quzah View Post
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com
    Code:
    do{
          
          printf("\n\nName:");
          scanf(" ");
          gets(employee[total_employees].name);
          printf("\nID:");
          scanf(" ");
          gets(employee[total_employees].id);
          printf("\nAge:");
          scanf(" ");
          gets(employee[total_employees].age);
    Why are those lines there?




    Quzah.
    A few weeks ago I told myself that I was learning to use fgets, but I forgot and I continued to use only gets...I'll look those links.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Running the above program is akin to playing Russian Roulette with memory.

    Get rid of the globals, especially if it's a big array of structs. Pass that around.

    Functions should always return something, and if not, specify void. Why is this error so common, I thought all modern compilers warned you?

    What if someone decides to enter a value longer than what you specified? I don't see any malloc() or free(), so I'm assuming it's all stack memory. It will crash and/or corrupt memory, due to the infamous gets() combined with your fixed-size char arrays. Furthermore, how long is your array of employee structs? What if someone enters more employees than possible? Set limits for both, and definitely use dynamic allocation. Otherwise, you're putting arrays of arrays all over the stack, which can't compensate for that much data reliably.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 10-03-2009, 10:58 PM
  2. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  3. Using string operations to save part of a file path
    By JackR in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2007, 03:48 PM
  4. Save long string of characters AND spaces
    By irsmart in forum C++ Programming
    Replies: 2
    Last Post: 09-29-2006, 02:57 PM
  5. save all text from file as string
    By GUIPenguin in forum C++ Programming
    Replies: 3
    Last Post: 05-04-2005, 05:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21