how to handle ints and strings when writing to file

This is a discussion on how to handle ints and strings when writing to file within the C Programming forums, part of the General Programming Boards category; Is it possible for one array to contain a string and few ints?? I want to write to a text ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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-16-2008, 11:11 PM
  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

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