• Advertisement
Sign in to follow this  

if statement problem

This topic is 4796 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am writing a little program that reads in a file and then outputs the contents to another file but ommiting new line characters, the problem is, the code is just creating a complete copy. here is the meat of it
character = getc(inFile);
	while(character != EOF)
	{
		if(character == '/n')
			;
		else
			putc(character, outFile);
		character = getc(inFile);
	}
[/Source]
[/source] Can you see what's wrong with that?

Share this post


Link to post
Share on other sites
Advertisement
my god, what an idiot, it's been a while lol.

Cheers.


Actually, what's ment to happen is my program takes a txt file and removes all \n from it, is there a faster way to do this than using a temp file, as at the end i delete the original file the rename the temp one.

Share this post


Link to post
Share on other sites
#include <algorithm>
#include <fstream>
#include <iterator>
#include <vector>

int main()
{
std::ifstream reader("file.txt", std::ios::binary);
reader.seekg(0, std::ios::end);
std::vector<char> data(reader.tellg());
reader.seekg(0, std::ios::beg);
reader.read(&data[0], data.size());
data.erase(std::remove(data.begin(), data.end(), '\n'), data.end());
reader.close();
std::ofstream writer("file.txt", std::ios::binary);
std::copy(data.begin(), data.end(), std::ostream_iterator<char>(writer));
}


Enigma

Share this post


Link to post
Share on other sites
cheers for the reply enigma, the only problem is, I am using C not c++ so I have to use fseek etc etc.

rating boost anyway as it was helpfull and I can use that type of thing in another project.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement