Delete a line in a file

This is a discussion on Delete a line in a file within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to delete a line in a file, but I'm not succeeding very well... Here is my function: ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    9

    Delete a line in a file

    Hi,

    I'm trying to delete a line in a file, but I'm not succeeding very well... Here is my function:
    Code:
    void deleteLine( int id )
    {
    	string contents;
    	int i = 1;
    	
    	ifstream file ( "file.txt" );
    	
    	while ( file.good() )
    	{
    		if ( i != id )
    		{
    			contents = sprintf("%s%s", contents, file.getline() );
    		}
    		i++;
    	}
    	
    	file.close();
    	
    	ofstream file ( "file.txt" );
    	
    	file << contents;
    	
    	file.close();
    }
    I get an error on contents = sprintf... Do you see any directly errors or shell I show my error messages too?

    Yours, @nders.
    Last edited by @nders; 12-30-2004 at 02:43 PM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    sprintf() returns an int. Trying to assign an int to a string is the likely cause of the error message you have posted.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Also, your using sprintf improperly. sprintf is not intended to be used to concatenate a file into one big string, like I think you want it to. Besides, your method will delete the entire file except for the last line, which is all that will be saved in contents.

    Instead of using sprintf, read the file using getline and use a string object which supports concatenation or create a buffer of sufficent size and manually concatenate the strings using strcat.

    Check out sprintf

    PK

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    9
    Thanks for the answer, elad!
    I edited my code, and got an error message like this:
    34 C:\[...]\program.cpp no matching function for call to `std::basic_ifstream<char,
    My new code looks like this:
    Code:
    void deleteLine( int id )
    {
    	string contents;
    	string buffer;
    	int i = 1;
    	
    	ifstream file ( "file.txt" );
    	
    	while ( file.good() )
    	{
    		if ( i != id )
    		{
    			file.getline( buffer, 100 );
    			sprintf( contents, "%s%s", contents, buffer );
    		}
    		i++;
    	}
    	
    	file.close();
    	
    	...
    }
    Line 34 is file.getline... Is somebody having an idea?

    Edit: Thanks, pablo615! I shell see what I can do.

    Yours, @nders.
    Last edited by @nders; 12-30-2004 at 02:43 PM.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You are using C++ strings, and there is a different version of getline that works with strings:
    Code:
    getline(file, buffer);
    Notice that the filestream is passed as an argument to the function, and the string buffer is also passed as an argument. Also notice that no max number of characters is needed (I removed the 100) because the string class grows automatically.

    Finally, since you are using the string class, there is no need for sprintf. Just use:
    Code:
    contents += buffer;
    [Edit] - Actually, one more thing. You are checking to see if the file stream is good after you use the results of the call to getline. You should call getline, then check if the file stream is good (or just use the return value of getline), then add the buffer to your contents only if the file stream is good.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    9
    Thank you, everybody! Of course I have a couple of questions now.

    I get
    Code:
    conflicting types for `std::ofstream file'
    when I try to compile. Can't I use same variable (or filestream, or what you call it :P) for both reading and writing?

    Quote Originally Posted by jlou
    [Edit] - Actually, one more thing. You are checking to see if the file stream is good after you use the results of the call to getline. You should call getline, then check if the file stream is good (or just use the return value of getline), then add the buffer to your contents only if the file stream is good.
    I don't think I really understood that. Can you please give me an example?

    Thank's again!

    Yours, @nders.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Quote Originally Posted by @nders
    Thank you, everybody! Of course I have a couple of questions now.

    I get
    Code:
    conflicting types for `std::ofstream file'
    when I try to compile. Can't I use same variable (or filestream, or what you call it :P) for both reading and writing?
    You cannot use the same variable name in the same scope. You are essentially telling the compiler that file is now an ofstream object when the compiler thought it was an ifstream object. This is illegal. Simply rename the second file to something else...

    PK

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    If you want to use the same variable for reading and writing, make it an std::fstream, not ifstream or ofstream. It would probably be wiser to use two different ones, though, since they will be used for different purposes.

    This FAQ applies to your problem with checking the file stream after using the data. The basic idea is that you are calling getline to get data into buffer. If getline fails, it returns the equivalent of false and sets a flag so that file.good() will return false. You must check that before you attempt to use what is in buffer, because there is nothing useful in buffer when the getline call fails.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    9
    Thank you very much for all help! The problem is solved now.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    19
    u can try this code it will work for u r case
    Code:
    void deleteLine(int id)
    {
    	char contents[500]="\0",buffer[500]="\0";
    	int i=1,;
    	ifstream file("file.txt");
    
    	while(file.good())
    	{
    		if( i != id)
    		{
    			file.getline(buffer,500);
    			strcat(contents,buffer);
    			strcat(contents,"\n");
    		}
    		else
    		{
    			file.getline(buffer,500);
    		}
    		
    		i++;
    	}
    
    	file.close();
    	
    	ofstream file1("file.txt");
    	
    	file1<<contents;
    	
    	file1.close();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21