Thread: how to handle ints and strings when writing to file

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    67

    how to handle ints and strings when writing to file

    Is it possible for one array to contain a string and few ints?? I want to write to a text file a string followed by a few ints, so they all appear on the same line in the file. Example of what I want to input:
    Code:
    string int int int \n
    The only way I know to write stuff into a file is doing it one array at a time, using fputs, but my problem is that fputs only deals with strings. So my question is how can I write a mixture of strings and ints to a file one line at a time and what functions would I use. Also if such thing can be done, would it then be problematic to extract them ?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by agentsmith View Post
    Is it possible for one array to contain a string and few ints?? I want to write to a text file a string followed by a few ints, so they all appear on the same line in the file. Example of what I want to input:
    Code:
    string int int int \n
    The only way I know to write stuff into a file is doing it one array at a time, using fputs, but my problem is that fputs only deals with strings. So my question is how can I write a mixture of strings and ints to a file one line at a time and what functions would I use. Also if such thing can be done, would it then be problematic to extract them ?
    Code:
    fprintf(file, "%s %d %d %d\n", the_string, val0, val1, val2);
    Perhaps?

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    Thanks. Now once its in the file do I treat it as a string when retrieving it?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by agentsmith View Post
    Thanks. Now once its in the file do I treat it as a string when retrieving it?
    Yes, it's a string once it passes through fprintf(). To convert back to string, int, int, int, you might be able to use fgets() followed by sscanf("%s %d %d %d") but there are caveats, namely if the string contains a space.

  5. #5
    abyss - deep C
    Join Date
    Oct 2007
    Posts
    46
    better still, you could declare a structure as per the requirements and write it to files

    Code:
    #define STR_SIZE 20
    
    typedef struct {
        char string[STR_SIZE];
        int a;
        int b;
        int c;
    }DATA;
    FILE *fp;
    DATA *item;
    
    fp = fopen("filename.txt", "w+");
    ...
    ...
    item = malloc(sizeof *item);
    ...
    ...
    strcpy(item->string, "Hello");
    item->a = 10;
    item->b = 20;
    item->c = 30;
    
    fwrite(item,sizeof(*item),1,fp);
    ...
    ...
    {
        DATA *temp;
        FILE *rfp;
        temp = malloc(sizeof *temp);
    
        rfp = fopen("filename.txt", "r");
        fread(temp,sizeof(*temp),1,rfp);
        printf("%s %d %d %d\n", temp->string, temp->a, temp->b, temp->c);
    }
    ...
    ...
    cheers
    maverix

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It need not necessary by on the heap, however:
    Code:
    #define STR_SIZE 20
    
    typedef struct {
        char string[STR_SIZE];
        int a;
        int b;
        int c;
    }DATA;
    FILE *fp;
    DATA item;
    
    fp = fopen("filename.txt", "w+");
    ...
    ...
    ...
    ...
    strcpy(item.string, "Hello");
    item.a = 10;
    item.b = 20;
    item.c = 30;
    
    fwrite(&item,sizeof(item),1,fp);
    ...
    ...
    {
        DATA temp;
        FILE *rfp;
    
        rfp = fopen("filename.txt", "r");
        fread(&temp,sizeof(temp),1,rfp);
        printf("%s %d %d %d\n", temp.string, temp.a, temp.b, temp.c);
    }
    ...
    ...
    This also solves your do not free memory case.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And those last two examples are creating a binary file, which is not always a good choice for saving data to a file - you can't for example use a text-editor to check it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's linux talk. The Windows way is that config files are not supposed to be edited directly. The application should handle those.
    Arguments against binary files is that it can break portability. A program that runs on OS X can't open config files created by the same program running under OS Y. Or the same logic with compiler X and compiler Y.
    But binary files are the easiest way of creating files, at least in C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Arguments against binary files is that it can break portability. A program that runs on OS X can't open config files created by the same program running under OS Y. Or the same logic with compiler X and compiler Y.
    That's true, though if say, the binary file was an SQLite database file, it would be consistent across platforms and examinable by any database viewing software that supports SQLite.
    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

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Agreed, databases are generally recommended. SQLite, which is written in C, can replace the standard fopen with its own functionality.
    It's a good thing since tie increases portability and makes it easier to manage complex files, registers, etc.
    It's just a shame no one is teaching their students to use a small database instead of files
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    That's linux talk. The Windows way is that config files are not supposed to be edited directly.
    But the simple fact that you can take a config file (or whatever the purpose of the file is).

    Yes, I agree, creating binary files is simpler - but it also makes it harder to "look at" the content of the file without using some code that knows the format of the file. It's requires more effort to add further fields to the file (assuming the input function is flexible).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    All these problems would nearly be solved by using a database. Gotta love those small buggers, eh?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 12-17-2008, 12:11 AM
  2. Array of Ints to a Strings
    By mattAU in forum C Programming
    Replies: 10
    Last Post: 08-17-2006, 05:25 AM
  3. writing strings chars from ints
    By deleeuw in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2003, 09:09 AM
  4. Strings into Ints
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2002, 04:07 PM
  5. converting strings to ints
    By HomerJ in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2002, 06:08 PM