-
Strange output..
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;
}
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.
Can anyone tell me what my errors are? Thanks.
-
what does your in file look like? I just wrote a simple file and it worked fine.
I would suggest not hardcoding the line count though... a simple function like this would work:
Code:
int countLines(char*buf)
{
int retval=0;
for(short int i=0;buf[i];i++)
{
if(buf[i]=='\n')
{
retval++;
}
}
return retval;
}
and then in main(), you'd call it like this:
Code:
WriteAllButOne(buffer,countLines(buffer));
also, try formatting your code better:
Code:
#include<iostream>
#include<cstdlib>
int WriteAllButOne(char* string, int line)
{
FILE * tempfile;
tempfile = fopen("test.out","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 countLines(char*buf)
{
int retval=0;
for(short int i=0;buf[i];i++)
{
if(buf[i]=='\n')
{
retval++;
}
}
return retval;
}
int main()
{
FILE * file1;
file1 = fopen("test.in","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,countLines(buffer));
fclose(file1);
delete[] buffer;
return 0;
}
-
Test.txt:
This is a test file
that I made
so that I could test
stuff
right?
0
0
0
0
hi
Output.txt:
This is a test file
so that I could test
stuff
right?
0
0
0
0
hiBor
-
here's my output:
Code:
This is a test file
that I made
so that I could test
stuff
right?
0
0
0
0
Try not hardcoding the line count, like i said... notice that the second line is missing in your output file.
also, take a look at the loop in this function:
Code:
int WriteAllButOne(char* string, int line)
{
FILE * tempfile;
tempfile = fopen("test.out","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;
}
if all you want to do is cut off at the last line, it may be better to write it as:
Code:
int WriteAllButOne(char* string, int line)
{
FILE * tempfile;
tempfile = fopen("test.out","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)
{
break;
}
fwrite(&string[x],1,1,tempfile);
printf("String[%i] is: %s\n",x,&string[x]);
x++;
}
fclose(tempfile);
return i;
}
-
Um, sorry if I wasn't clear, but the whole point of that function is to write all of the lines in a text file except for one specified line. I didn't add a hard limit for the number of lines that I'm aware of, the "int line" in WriteAllButOne(); is the line to be omitted.
-
ah okay I see now. I thought you meant except the last line, but I was wrong. in that case, keep it written the way you had it. it worked both ways for me. It's still hard-coded in there, but in this case it's probably not a big deal.
with this code I have no problems (note that it's mostly the same as the code in your OP)
Code:
#include<iostream>
#include<cstdlib>
int WriteAllButOne(char* string, int line)
{
FILE * tempfile;
tempfile = fopen("test.out","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.in","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;
return 0;
}
the only changes I can remember are the removal of getchar() and commenting out of debugging output.
edit: It could be that maybe buffer isn't null-terminated? try allocating size+1 chars and appending '\0' to the end of the buffer (or better, intializing the buffer with '\0' instead of '0'.
here are the three changes you'd have to make (in main())
Code:
char* buffer = new char[size+1]; //just add the +1
int x = 1;
while (x < size+1) //again, just the+1
{
buffer[x] = '\0'; //change 0 to '\0'
x++;
}
-
Code:
#include <cstdio>
#include <windows.h>
int WriteAllButOne(char* string, int line)
{
FILE * tempfile;
tempfile = fopen("output.txt","wb");
if (tempfile == 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 (file1 == NULL) {
return 0;
}
fseek(file1,0,SEEK_END);
int size = ftell(file1);
rewind(file1);
printf("%i\n",size);
char* buffer = new char[size+1];
int x = 0;
while (x < size+1)
{
buffer[x] = '\0';
x++;
}
fread(buffer,1,size,file1);
printf("%i",size);
WriteAllButOne(buffer,2);
fclose(file1);
delete[] buffer;
getchar();
return 0;
}
Here's what I have now, it all seems to work fine. Thanks for your help! I also fixed a bug where it crashes when it doesn't find the file (I had put fopen in the error checking instead of the right variable)
Again, thanks a lot!