Thread: How to save array to file

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    17

    How to save array to file

    There is a simple C-code, where I defined array of string type. Need to save it into ASCII-file (later I will need to read array back from this file). I know that I did something wrong in 'fwrite'. Please help me to find the problem and fix it.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    char* a[2]      = {"130123456789", "2"};
    
    
    int main(int argc, char* argv[]) {
    
        printf("\n a[0] = %s", a[0]);
        printf("\n a[1] = %s", a[1]);
    
    
    // ====================================================
    
        FILE *file_01;
    
        if((file_01   = fopen("OUT_a.txt","w"))==NULL) {
            printf("CANNOT OPEN FILE OUT_a.txt 'w'\n");
            exit(1);
        };
    
    
        printf("\n strlen(a[0]) = %i", strlen(a[0]));
        printf("\n strlen(a[1]) = %i", strlen(a[1]));
    
    
        int t1 = fwrite(*a, (strlen(a[0])+strlen(a[1])), 1, file_01);
    
        printf("\n t1 = %i", t1);
    
        fclose(file_01);
    
    // ====================================================
    
        printf("\n\nFINISH!\n");
    
        return 0;
    }

  2. #2
    Registered User john.c's Avatar
    Join Date
    Dec 2017
    Posts
    554
    a is an array of two pointers and you are writing 13 bytes starting at the first pointer. If the pointers are 8 bytes each, you are writing the first 13 bytes of the pointer values. If they are 4 byte pointers then you are writing all 8 bytes of the two pointers plus 5 bytes of whatever happens to be directly after them in memory. You are not writing the strings at all (unless you have 4-byte pointers and part of the strings are in those 5 extra bytes).

    Normally you don't use fwrite to write to a text file. fprintf is more usual.
    Code:
    fprintf(file_01, "%s\n%s\n", a[0], a[1]);
    Let him who is not come to logic be plagued with continuous and everlasting filth.
    - John of Salisbury, 1160

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    17
    Quote Originally Posted by john.c View Post
    a is an array of two pointers and you are writing 13 bytes starting at the first pointer. If the pointers are 8 bytes each, you are writing the first 13 bytes of the pointer values. If they are 4 byte pointers then you are writing all 8 bytes of the two pointers plus 5 bytes of whatever happens to be directly after them in memory. You are not writing the strings at all (unless you have 4-byte pointers and part of the strings are in those 5 extra bytes).

    Normally you don't use fwrite to write to a text file. fprintf is more usual.
    Code:
    fprintf(file_01, "%s\n%s\n", a[0], a[1]);
    I found the following example in the internet:

    Code:
    int main()
    {
        std::string array[] = { "S1", "S2", "S3" };
        std::cout << "A number of elements in array is: "
                  << sizeof(array)/sizeof(array[0]) << '\n';
        foo(array);
    }

    The most interesting to me is the following line:

    Code:
    string array[] = { "S1", "S2", "S3" };
    I tried to use 'string array[]...', but my compiler DevC++ didn't accept it. Maybe you know why?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,319
    That's C++ code, not C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. save a spicific column from a csv file into an array
    By Tasneem Gamal in forum C Programming
    Replies: 5
    Last Post: 09-07-2017, 11:57 AM
  2. Replies: 0
    Last Post: 03-21-2013, 03:31 PM
  3. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  4. Open File/Save File crashes on cancel?
    By Blackroot in forum Windows Programming
    Replies: 0
    Last Post: 08-02-2008, 02:16 AM
  5. Save array
    By eletron in forum C Programming
    Replies: 3
    Last Post: 02-24-2005, 03:47 PM

Tags for this Thread