Thread: String I/O issues - need help

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    7

    String I/O issues - need help

    I am a novice with respect to C programming. This is my first semester in college that I've taken a C class. I apologize if my question is a bit rudimentary.

    The program I'm working on is an assignment that reads a text file and compares a word bank of strings to text messages to check for misspelled words and such. The first line in the input file is n numbers of words in the "dictionary" (the proper words which are used for comparison). The next n lines are the words contained in the dictionary.

    I'm having trouble reading these strings into a 2-dimensional array with n rows and 30 columns (the words cannot be more than 29 characters + 1 null character). Here's what I have:

    Code:
    int main() {
        FILE *textMsg; // Text input file pointer
        int n, m, t, w; // Asignment variables from in-text values
        int i, j, k; // Counter variables
        char c;
    
        if((textMsg = fopen("textmsg.txt", "r")) == NULL ){
            printf("Sorry, the file could not be opened.\n\n");
            system("PAUSE");
            return 0;
        }
        
        fscanf(textMsg, "%d", &n); // Get number of words in the dictionary
    
        char dictionary[n][30]; // Declares the array to store the dictionary words
    
        while(!feof(textMsg)){
            for(i = 0; i <= n; ++i){
                for(j = 0; j <= 30; ++j){
                    fgets(*dictionary, j, textMsg);
                } 
            } 
        } 
    
        fclose(textMsg);
    
        system("PAUSE");
    
        return 0;
    }
    I have obviously included the string.h header. I was using the fgets() function to read each line until the new line character, hoping that it would only store that many characters and then move onto the next interation in the loop, but its reading the entire file, not n lines of text and <30 characters per line. The dictionary words are only a small chunk of the entire file so there will be multiple arrays in the final product which is why I'm not interested in reading the entire file.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    methinks you don't need the inner for loop; store every word (<=29 chars) into each of the elements of dictionary[], as in:
    Code:
    for (i = 0; i < n; ++i)
        fgets(dictionary[i], sizeof dictionary[i], textMsg);

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    7
    That still reads the entire file when I try to print the contents of dictionary[i].

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Isn't each line of the input file a dictionary word <= 30 characters ??

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    7
    Not necessarily. It's only supposed to read a maximum of 29 characters. And theres a lot of text in the input file, but I only want to read the first n lines for the dictionary array.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    can you post a sample of the input file

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    7
    This is an example that the professor made:

    4
    i
    jason
    love
    you
    2
    ahole
    jerk
    5
    4:21 AM
    4 Jason I LOVE you
    3:34 PM
    4 Jason I love you
    2:00 AM
    4 Jsoan I lve you
    4:00 AM
    3 Jason you ahole
    7:00 AM
    3 Jason you jerk


    4 is the number of dictionary words, 2 is the number of forbidden words, and 5 is the number of text messages that the program has to check for misspelled and swear words.

    At this point, I get n which is 4 and then I only want to read the next 4 lines into the dictionary array.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    At first I didn't notice that the two for loops were enclosed in a while loop.
    In any case feof() is not a good test for the terminal condition, see this site's faq as to why.
    Code:
    while(!feof(textMsg)) {
            for(i = 0; i <= n; ++i){
                for(j = 0; j <= 30; ++j){
                    fgets(*dictionary, j, textMsg);
                } 
            } 
    }
    Replace the entire code snippet above with the for loop below.
    Code:
    for (i = 0; i < n; ++i)
        fgets(dictionary[i], sizeof dictionary[i], textMsg);

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Note that fscanf() stops when it sees the newline at the end of the first line so skip over it before calling fgets() otherwise the first string read will be NULL.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    7
    Can you explain that?

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Change your fscanf() call so that there's trailing whitespace after the %d conversion spec.
    Code:
    fscanf(textMsg, "%d ", &n);

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    7
    I did that and I still get 4 NULLs printed out.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by thatzilla View Post
    I did that and I still get 4 NULLs printed out.
    can you post your updated code!

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    7
    Nevermind I got it! Thanks man!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM