help with read/write file/array of chars

This is a discussion on help with read/write file/array of chars within the C Programming forums, part of the General Programming Boards category; Hello, Im trying to figure out how write a string into a file, line like this: Code: string1; string2; string3; ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    3

    Question help with read/write file/array of chars

    Hello, Im trying to figure out how write a string into a file, line like this:

    Code:
    string1; string2; string3;
    string4; string5; string6; ect
    after this I need to read file line by line and put everithing in to array, like this:

    Code:
    array1[100]; array2[100]; array3[100];
    array4[100]; array5[100]; array6[100];
    notice that the string not always have 100 chars because I use this function:
    Code:
    void ReadString(char caracteres[100])
        {
        for (i=0; i < 99; i++)
            {
            c = getchar();
            if (c == '\n') {caracteres[i] = '\0'; break;}
            caracteres[i] = c;
            }
        }
    Thank u!

  2. #2
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    there are more than one way u can use to read and write from files. when dealing with C i prefer to use either fwrite/fread or fprintf/fscanf.

    Code:
    FILE * f = fopen("file.txt", "w+t");
    char str[] = "Hello World";
    if ( f != NULL ) {
         fwrite(str, sizeof(char), sizeof(str), f);
         fclose(f);
    }
    f= fopen("file.txt", "r+t");
    fseek(f, 0, SEEK_END);
    size_t sz = ftell(f);
    char * getStr = (char *)malloc(sizeof(char) * (sz));
    fread(getStr, sizeof(char), sz, f);
    printf("The string read was %s\n", getStr);

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    Thnx, but what Im trying to do, is to read 1st string from file before ";" and save it in array, than read 2nd string before ";" and save to another array.
    I dont know how to read until some character, than save to array and that to continue reading of file untill the end.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Currently you are checking for new-line (with c == '\n'). Why not check for semicolon instead?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    Currently you are checking for new-line (with c == '\n'). Why not check for semicolon instead?
    And then get the REST of the line right behind it...

    It would be smarter to use something like fgets(), strip the file line by line into a buffer then parse the buffer for the desired text.

  6. #6
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    or get the whole line and use the strtok function on it

    Code:
    //#include <string.h> 
    char * str = strtok(caracteres, ";");
    while (!str) {
      printf("%s\n", str);
      str = strtok(NULL, ";");
    }

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    3

    Smile

    Quote Originally Posted by CommonTater View Post

    Quote Originally Posted by tabstop
    Currently you are checking for new-line (with c == '\n'). Why not check for semicolon instead?
    And then get the REST of the line right behind it...
    It would be smarter to use something like fgets(), strip the file line by line into a buffer then parse the buffer for the desired text.

    The problem is, that Im just learning C and my teacher didnt explane well all the stuff, we are still on scanf and prinft crap...
    So all the code, I get from internet and implement it in my program, anyway thnx for advice!



    As for codeprada, thnx I think its what I was looking for.





    Code:
    Sorry about my horrible english xD

  8. #8
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    sure no prob....feel free to message me if you need any help

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by JustJonny View Post
    The problem is, that Im just learning C and my teacher didnt explane well all the stuff, we are still on scanf and prinft crap...
    So all the code, I get from internet and implement it in my program, anyway thnx for advice!
    No problem getting ahead of your teacher... all good students do.

    BUT... you aren't going to learn a darned thing using scoop and poop code. Really, that ultra nifty little game you just finished with "borrowed" code... how well do you actually understand how it works?

    Instead of wasting your time searching the internet for code snippets, get busy with the books and keyboard... you learn coding by writing code... read the books, do the exercises... and write your own code. You'll be a much better programmer for it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Signed/Unsigned Chars
    By ankitsinghal_89 in forum C Programming
    Replies: 16
    Last Post: 06-07-2010, 12:22 PM
  2. Filter invalid chars such as 007F,...
    By securelinprog in forum C Programming
    Replies: 14
    Last Post: 04-08-2010, 12:29 AM
  3. condition variable on read/write locks
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 04-29-2009, 10:32 AM
  4. while condition question..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 05:49 PM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 05:29 PM

Tags for this Thread


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