Sign in to follow this  
JinJo

if statement problem

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this