Hi, I'm having trouble with a java program I was tasked with converting to C, the original program take an in.txt file filled with rougthly a paragraph's worth or words, each on it's own line and outputs to a out.txt file the same words now on consistent lines no longer than 50 chars, however my current code, only adds a space to each line and then repeats the last word forever, causing me many headaches...
I want to learn from my mistakes so please point me in the right direction, I'd rather write the correct code myself instead of having it handed to meCode:#include <stdio.h> #include <string.h> int main() { int LINE_SIZE = 50; // this will decide how long our lines can be int Position; char InputLine[50]; //Creates the pointers needed to reference our files FILE *FileIn; FILE *FileOut; //open the files FileIn=fopen("in.txt","r"); FileOut=fopen("out.txt","w"); //check to see if they opened correctly if(FileIn==NULL) { printf("Error: can't open the input file.\n"); } if(FileOut==NULL) { printf("Error: can't open the output file.\n"); } //reads one line at a time from in.txt fscanf(FileIn, "%s", &InputLine); //I know the "Position != feof(FileIn)" is wrong but I'm unsure as to what should go there, would it be "InputLine != feof(FileIn)"? for (Position=1; Position != feof(FileIn); Position++) { if (InputLine == (NULL)) // I suspect this is also causing some trouble, it's supposed to check to see if i'm at the end of the line, the original program had ("") instead of NULL but that didn't work { if (Position > 1) { fprintf(FileOut, "\n"); } fprintf(FileOut, "\n"); Position = 1; } else { //checks to see if my current line is over the 50 char limit if ((Position+(strlen(InputLine))-1) > LINE_SIZE) { fprintf(FileOut, "\n"); Position = 1; } // print the word to out.txt in the correct format fprintf (FileOut, "%s\n", InputLine); Position += (strlen(InputLine)); if (Position <= LINE_SIZE) { fprintf (FileOut, " "); Position++; } } fscanf(FileIn, "%s", &InputLine); } fclose(FileIn); fclose(FileOut);



LinkBack URL
About LinkBacks



