gathering input as array

This is a discussion on gathering input as array within the C Programming forums, part of the General Programming Boards category; heyas, i am having some trouble with understanding how i can overcome a problem. Currently, i have a file named ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    35

    Talking gathering input as array

    heyas, i am having some trouble with understanding how i can overcome a problem. Currently, i have a file named 'woop.txt' , stored in the c:\. The contents of the file is 'hello'. I wish to store this contents as an array, named 'word'. The following code is what i wrote to gather the file contents and print it on the screen, this code compiles successfully and has no problems.

    int main()
    {
    FILE *fp;
    int c;

    fp = fopen("woop.txt", "r");
    while((c = getc(fp)) !=EOF)
    putchar(c);
    fclose(fp);
    getch();
    }

    Now, if i have:
    char word[5];
    definied, in order to store the contents of each letter in the array, i am required to have a for loop (could be wrong! :P)

    I already have a while loop running, however, and if i have

    while((c = getc(fp)) !=EOF)
    fscanf("%c", &word[]);
    it generates an error. my code for the initial output of the file works with 'c' storing the contents of each individual character, i want to store each one of those characters in word[].

    the only experience i have with such storing would be to have a for loop as follows:
    for(x=0;x<5;x++)
    scanf("%c", word[x]);
    this, mixed with my original code will create problems, however, due to the embeded loops of a while>for:
    while((c = getc(fp)) !=EOF)
    for(x=0;x<5;x++)
    fscanf("%c", word[x]);
    this code is obviously not going to work.

    please give me any ideas or help
    thanks in advance

    -twans

  2. #2
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    This should work to write to the array but I don't have time to test it at the moment.

    Code:
     
    fp = fopen("woop.txt", "rb"); 
    while((c = getc(fp)) != EOF)
    word[i++] = c;
    fclose(fp);
    Notice that I changed your file permission (writes in UNIX) to read in binary mode. I recommend you do this as ASCII files tend to be self destructive. That shouldn't be a problem with a file this small though. Hope that helps, Good Luck.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    35


    could you please explain in more detail the line you added:

    word[i++] = c;

    theres the array of type char, and an integer i?
    and your assigning the word[] arrray to c, then the 'i++' moves to the next value in that array, which would be word[1], then, again the value of c would be held and the 'i++' moves the word array to word[2].. and so on

    is this correct? if so, could you please give me a little explination?
    thanks a lot for your reply =)

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What happens is this:

    c = fgetc( filePointer ); // read one byte into 'c'
    word[i++] = c; // put the value of c into 'word[i]' then increment i.

    This loops until the end of the file. This is wrong. The reason it is wrong is that you'll soon run off the end of your array if your file is bigger than said array. Thus, it should be:

    while ( i < array_size && (c=fgetc( filePointer )) != EOF )
    word[i++] = c;

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    Thanks Quzah. I wrote that reply to Twans question at about 3am .. Probably not a good idea lol. For some reason, I was just assuming that he was always going to run the program with a file of the same size. Thanks again for the correction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Replies: 2
    Last Post: 03-12-2002, 02:32 PM

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