Thread: writing array into file and reading array from file

  1. #1
    Registered User
    Join Date
    Apr 2014
    Location
    Petaling Jaya, Malaysia, Malaysia
    Posts
    1

    writing array into file and reading array from file

    i've been trying to write array into file and read them into the same program again, but the output is all wrong. i've googled, but i just can't find the solution. please help point out my mistakes. thank you.

    Code:
    #include<stdio.h>
    int main()
    {
        FILE *fp;
        char name[3][7];
        int x;
        
        fp=fopen("one.txt","w");
        printf("\n Enter name:\n");
        for(x=0;x<3;x++)
        {
            gets(name[x]);
            fprintf(fp,"%s",name[x]);
        
        }
        
        
        fclose(fp);
        
        fp=fopen("one.txt","r");
        for(x=0;x<3;x++)
        {
            fscanf(fp,"%s",name[x]);
            printf("\n Name[%d] entered: %s\n",x+1,name[x]);
        }
        
        
        fclose(fp);
        
        system("pause");
        return 0;
    }
    output:
    Enter name:
    lily
    kiki
    mimi

    Name[1] entered: lilykikimimi
    Name[2] entered: kiki
    Name[3] entered: kiki

    p/s: i learn from you tube videos only since my school doesn't cover the topic yet. please help.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    1) Don't use gets(). Look up the fgets() function.

    2) The code that's writing the file is just writing the strings, with nothing in between. So, if three names "lily", "bob", and "pete" are entered, the file will contain the string "lilybobpete". fscanf() %s format will interpret that as one string, not three. When writing each string, output whitespace (a space character, a newline, etc) - as that is what will cause fscanf() to stop reading for %s. fgets() will accidentally make this easier for you, since the string it reads will have the newline appended (unless the string entered is too long).

    You could have worked this out by actually opening the file "one.txt" with some other program (like a text editor).

    Bear in mind that %s will just keep going until it finds whitespace (or an error occurs), regardless of the length of the string buffer supplied for reading to. So, if the buffer is 4 characters long, use %3s (to allow for the terminating null).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing an array in a file
    By Marria in forum C Programming
    Replies: 13
    Last Post: 09-12-2012, 02:25 PM
  2. Writing array to a file
    By redsfan in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2011, 05:01 PM
  3. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  4. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  5. Writing an array to a file
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 10-24-2005, 06:25 AM