Thread: populating an array from a text file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    42

    populating an array from a text file

    First off, I do have this questions posted on another site but I have yet had a suggestion on what my problem might be. I can also add attachments here which may be helpful.

    I have this program for class where we just need to show that we can read, write to a text and binary file. We also need to populate an array from both a text and binary file. When the user first runs the program, the user is told that text/binary file does not work for reading because they do not exist. The user is then asked to start inpuyting integers into the console and at the same time creating and appending to a text/binary file. These values are also stored into an array as they are being inputted. Later in the program the array is run through a for loop and displayed to the screen. Displaying the array to the screen works fine here.

    My problem is when the user runs the program for a second time. The text file is opened and an fgets is run to populate an array based on the numbers in the text file. That array is then used again for the user input and file writing section of the program. When the array is displayed at the end of the program the numbers do not make sense. Some appear to be correct bu most do not. When I do the similar actions to the binary file there is no problems. The text file is amended properly everytime the user runs the program. userInputText[MAXSIZE]is a char array (MAXSIZE is defined at 100000) and both textCount and textCounter are initialized to zero when the program is at this point.

    Here is what the code where I believe the program resides. I have also attached the source file.

    Thank You,

    Mike

    Code:
    textPointer = fopen("textFile.txt", "r");
    
    
         if (textPointer) {
                    
               while(fgets(userInputText, sizeof(int), textPointer) != NULL){
                     sscanf(userInputText, "%d", &textCount);
                     userInputText[textCounter] = textCount;
                     textCounter += 1;
                        
         }
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Populating an Array
    By amilie1234 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2012, 09:35 PM
  2. Populating Array Difficulty
    By mcertini in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2010, 12:42 AM
  3. from text file to array
    By apisio in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2009, 07:34 AM
  4. populating 2-D array
    By edster in forum C Programming
    Replies: 4
    Last Post: 10-22-2009, 02:42 PM
  5. Populating a multi-dimensional array problem
    By Zildjian in forum C Programming
    Replies: 7
    Last Post: 10-06-2003, 08:17 AM