0

I have this program for class where we just need to show that we can read, write and populate an array from both a text and binary file. I didnt have many issued with the binary file but the text file is giving big time brain pains.

I believe this little bit of code is to blame. 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.

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

When the user first runs the program, this part of the program is skipped and the user is asked to create and write to a text file. The user inputs integers and they are written to a text file and stored in userInputText and displayed to the screen at the end of the program. All of that works fine, the text file is appended correctly every time the program runs. I have tried about every combo I can think of in the above piece of code. I have tried using fopen(r+), sizeof(int and MAXSIZE), experimenting with the %d. In my fprintf part later in the program I have "\n" set as a delimeter, but I have also tried removing it. I switched out the fgets for fread. Ive gotten to the point where I'm resembling a baboon throwing poo at the problem.

I don't want people to think I am lazy and looking for an easy answer, mentally handicapped maybe but not lazy.

Any suggestions?

Thanks,

Mike

5
  • What actually happens when you run the program? Commented Feb 2, 2013 at 2:31
  • 1
    What exactly are you trying to do? This code attempts to read an integer from formatted text-input of a FILE* stream, then for some odd reason you update the same text buffer that you're using for line processing with the very value you just read as text, but now with the int converted result. If that is your intention (and I can't see how it could be), so be it, but you do realize you're blowing away the very value you just saved with each new line read, right? Commented Feb 2, 2013 at 2:39
  • When I run the program the first time. It asks the user to input numbers. I do a scanf(%d) in a while loop until the user enters -999. While user is in loop, numbers are stored to userInputText[] and counter is incremented for subscript. I then use fprintf(textPointer, "%d\n", textTemp); to write file to text file. The array and text file are fine up until this point. Its the next program run where I have issues, because before allowing the user to input more numbers, I have to populate an array based on the text file. Commented Feb 2, 2013 at 3:31
  • 1
    @MichaelBrooks I understand. What I'm saying is you don't have to populate your line-read buffer as the array you're accumulating your int-conversions with. As written you store the int-converted value from the prior read back into the line buffer, then immediately throw that buffer back into fgets() where the data you just stored will promptly be overwritten. Use a different array for storing your converted integers. Commented Feb 2, 2013 at 4:22
  • Thanks Craig, along with creating another array it also had to be an int array. When I first started with the program, I was trying to use an int array but I kept trying to use it as an argument in fgets which only takes char arrays. I appreciate it Commented Feb 2, 2013 at 7:16

1 Answer 1

0

Try while( !feof(textPointer) ) { fgets(userInputText,MAXSIZE,textPointer); }

sizeof(userInputText) will only return the size of a char pointer.

Sign up to request clarification or add additional context in comments.

3 Comments

This is not true if userInputText is declared as a fixed array, which the OP specifically states it is in the question.
I hope I'm answering everyone sufficiently here. My instructions are, to create an integer array to store ip to 5,000 numbers and allow the user to input numbers into an array and save the numbers to a file. When the user runs the code for the second time, I need to populate an array based on the numbers saved to a file. Later in the program the user should be able to enter in more numbers that will take over where the populated array left off.
The code needs to write and read from both a text file and a binary file. I did the same code with some varriation (fread, fwrite etc..) for my binary file and it worked great. The code I originally posted should (after the first run) get the file and populate an array. If there are 20 numbers in the file. 10,20,30etc.. I should go to the input portion of the program with userInputText[20] = textCount and than start adding to the array. I would post the whole program but it kind of lengthy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.