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;
}