Thread: Writing an array in a file

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    8

    Writing an array in a file

    Hello everyone! I'm not quite familiar with programming, so please go easy with me
    I'm trying to write an array into a file and this is what i come up with:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char words[][2][40]={
          "adorer","adore",
          "gras","greasy",
          "outil","tool",
          "radis","radish",
          "tracer","trace",
          "",""
    };
    
    main()
    {
    
    
        FILE *fp;
        int i;
    
        if(( fp=fopen( "myfile", "w" )) == NULL) {
            printf("Cannot open file.\n");
            exit(1);
        }
    
        fprintf( fp, "%s", words[i]);
        fclose(fp);
    
        if(( fp=fopen( "myfile", "r" )) == NULL) {
            printf("Cannot open file.\n");
            exit(1);
        }
    
        fscanf( fp, "%s", words[i]);
        printf(" %s", words);
        fclose(fp);
    
        return 0;
    }
    But it's not working. Any suggestions where am i wrong will be great.
    By the way i'm running it on Turbo C++ 4.5, my operating system is XP and it gives me : "General Protection Exception... Processor Fault" and than i have to restart the compiler.

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Code:
    char words[][2][40]
    ...
        fprintf( fp, "%s", words[i]);
    words[i] is not a string: it's an array of 2 strings (each of them with a maximum of 39 characters).

    Try this
    Code:
        fprintf( fp, "%s / %s\n", words[i][0], words[i][1]);
    Note that words[i][0] (and words[i][1]) is indeed a string.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    Three suggestions:
    1. Turn on all warnings in your compiler. Warnings usually tell you that you're doing something wrong.
    2. Fix each problem pointed out by the warnings. If you don't understand what a warning means or how to fix it, feel free to ask about it here, but be sure to copy and paste the complete warning and the complete program (if you made any changes since the first version you posted).
    3. Get a different (better) compiler. Turbo C++ has a bad reputation among some people around here (I've never used it so I only know about its shortcomings secondhand).

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    Quote Originally Posted by qny View Post
    Code:
    char words[][2][40]
    ...
        fprintf( fp, "%s", words[i]);
    words[i] is not a string: it's an array of 2 strings (each of them with a maximum of 39 characters).

    Try this
    Code:
        fprintf( fp, "%s / %s\n", words[i][0], words[i][1]);
    Note that words[i][0] (and words[i][1]) is indeed a string.
    When i do that and when i run the program(without errors) it only shows "adorer" on the screen and there is no text file in the directory.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by Marria View Post
    ... it only shows "adorer" on the screen ...
    You need to print (and write to the file) in a loop.

    Quote Originally Posted by Marria View Post
    ... there is no text file in the directory.
    Perhaps you meant "myfile.txt" rather than "myfile" with no extension?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by qny
    words[i] is not a string: it's an array of 2 strings (each of them with a maximum of 39 characters).
    It's actually 40. Just because the first one is at subscript 0 doesn't change the total. C isn't messing with you.

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by whiteflags View Post
    It's actually 40. Just because the first one is at subscript 0 doesn't change the total. C isn't messing with you.
    The first one being 0 is not the problem. The problem is the last one (at whatever position it happens to be, including position 39) having a value of 0 and not counting towards the length of the string.

    The string "hello" has 5 characters, but it is an array of size 6.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well I guess that's a way of looking at it.

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    Code:
    if(( fp=fopen( "myfile", "w" )) == NULL) {
            printf("Cannot open file.\n");
            exit(1);
        }
             for(i=0; words[i][0]; i++){
                for(j=0; words[i][j]; j++){
                    fprintf( fp, "%s, %s", words[i][0],words[i][1]);
              }
            }
    That's how i do the loop, but... nothing(i'm not really sure if its right) and there is no "myfile.txt" there is "myfile" and i opened it with notepad and there was a "comma".
    Last edited by Marria; 09-12-2012 at 09:36 AM.

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    Christop i really want to change this compilar but the professor uses and so am i. As for the warnings - i'm still searching the answers in the net

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Stick with what you are using now. Don't get caught doing a good program, and then having it marked down because you used a different compiler than the one the professor is using.


    Give this a look over with your compiler, and see how it does:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    char words[][2][40]= {
          {"adorer","adore"},
          {"gras","greasy"},
          {"outil","tool"},
          {"radis","radish"},
          {"tracer","trace"},
          {"",""}
    };
     
    int main(void)
    {
     
     
        FILE *fp;
        int i;
     
        if(( fp=fopen( "myfile", "w" )) == NULL) {
            printf("Cannot open file.\n");
            exit(1);
        }
        //print out the original string array to the file
        for(i=0;i<6;i++) {
           fprintf( fp, "%s %s ", words[i][0], words[i][1]);
        }
        fclose(fp);
       //show the original string array 
       for(i=0;i<6;i++) {
           printf( "%s, %s, ", words[i][0], words[i][1]);
        }
    
        if(( fp=fopen( "myfile", "r" )) == NULL) {
            printf("Cannot open file.\n");
            exit(1);
        }
     
       //reread the file strings
       for(i=0;i<6;i++) {
        fscanf( fp, "%s %s ", words[i][0], words[i][1]);  
       }
       fclose(fp);
    
       printf("\n\n Reloaded values from the file are:\n");
        for(i=0;i<6;i++) {
           printf( "%d: %s, %s, \n", i,words[i][0], words[i][1]);
        }
       printf("\n");
     
        return 0;
    }

  12. #12
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    The only real thing wrong with TurboC is it's from the 1980's. Compilers have made a few advancements since then.

    Why call exit(1) when return 1 will do it without the stdlib.h header?

    Looks like you've made changes. You need to post the code again so we know what you're doing.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    Adak it works perfectly fine! Thanks a lot!
    WaltP I don't use return 1, because in my textbook it is exit(1), but i'll remember your advice.
    I have to present my project on Turbo C++, because the prof. want it on Turbo C++ and this program is a little part of it.
    And one more time - thank you for your help
    Last edited by Marria; 09-12-2012 at 11:27 AM.

  14. #14
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    FYI, textbooks don't always use the best examples...

    We still don't understand why many schools/universities insist on using 1980's technology when there are so many compilers that are up-to-date and free. We also realize neither do you, so no need to answer. Unless, of course, up can actually find out for us.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing array to a file
    By redsfan in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2011, 05:01 PM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  3. Writing an array to a file
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 10-24-2005, 06:25 AM
  4. Writing an array to a file.
    By Queatrix in forum C++ Programming
    Replies: 8
    Last Post: 04-24-2005, 01:41 PM
  5. writing contents of array to file
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-26-2002, 04:06 PM