Well, I am trying to make a function that loads a text file into memory, and then writes it all back, minus one line. This is the code I have, it gives some unexpected output, and usually there are some random characters at the end of the new file.Code:#include <cstdio> #include <windows.h> int WriteAllButOne(char* string, int line) { FILE * tempfile; tempfile = fopen("output.txt","wb"); if (fopen == NULL) { return 0; } int x = 0; int i = 1; int maxlen = strlen(string); while (x < maxlen) { if (string[x] == '\n') { i++; } if (i != line) { fwrite(&string[x],1,1,tempfile); printf("String[%i] is: %s\n",x,&string[x]); } x++; } fclose(tempfile); return i; } int main() { FILE * file1; file1 = fopen("test.txt","rb"); if (fopen == NULL) { return 0; } fseek(file1,0,SEEK_END); int size = ftell(file1); rewind(file1); printf("%i\n",size); char* buffer = new char[size]; int x = 1; while (x < size) { buffer[x] = 0; x++; } fread(buffer,1,size,file1); printf("%i",size); WriteAllButOne(buffer,2); fclose(file1); delete[] buffer; getchar(); return 0; }
Can anyone tell me what my errors are? Thanks.



LinkBack URL
About LinkBacks


