reading into array

This is a discussion on reading into array within the C Programming forums, part of the General Programming Boards category; hi, i posted yesterday asking about reading words from a file into an array. Someone (thank you whoever you are!) ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    reading into array

    hi, i posted yesterday asking about reading words from a file into an array. Someone (thank you whoever you are!) suggested the following code:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main (void)
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          int ch, word = 0;
          while ( (ch = fgetc(file)) != EOF )
          {
             if ( isspace(ch) || ispunct(ch) )
             {
                if ( word )
                {
                   word = 0;
                   putchar('\n');
                }
             }
             else
             {
                word = 1;
                putchar(ch);
             }
          }
          fclose(file);
       }
       return 0;
    }
    This sends the words to standard output but i want to store each word in an array in a linked list, how would i do this. Also, would it be better to use fscanf - if so what would the code look like?

    thanks a lot

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    So replace the putchar calls with code to build a string character by character. Show some attempt, please.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data into a 2D array
    By swgh in forum C Programming
    Replies: 2
    Last Post: 08-17-2007, 03:07 AM
  2. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Reading a mixed Text and decimal file into an array
    By djamie in forum C Programming
    Replies: 3
    Last Post: 08-05-2003, 06:25 AM
  5. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM

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