Thread: reading into array

  1. #1
    Registered User
    Join Date
    Jan 2006

    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:

    #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;
                word = 1;
       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
    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, 12:09 PM
  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