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
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 theintconverted 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?int-conversions with. As written you store theint-converted value from the prior read back into the line buffer, then immediately throw that buffer back intofgets()where the data you just stored will promptly be overwritten. Use a different array for storing your converted integers.