Thread: Strange output..

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    6

    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.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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;
    }
    Last edited by major_small; 08-23-2005 at 03:22 AM. Reason: color-happy syntax highlighter.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6
    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

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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;
    }
    Last edited by major_small; 08-23-2005 at 03:23 AM. Reason: color-happy syntax highlighter.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6
    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.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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++;
    	}
    Last edited by major_small; 08-23-2005 at 03:46 AM. Reason: color-happy syntax highlighter.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Output an array in a textbox
    By Diablo02 in forum C# Programming
    Replies: 5
    Last Post: 10-18-2007, 03:56 AM
  2. Binary Search - Strange Output
    By mike_g in forum C Programming
    Replies: 13
    Last Post: 06-16-2007, 02:55 PM
  3. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  4. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 06:46 PM
  5. Really strange, unexpected values from allocated variables
    By Jaken Veina in forum Windows Programming
    Replies: 6
    Last Post: 04-16-2005, 05:40 PM