Skip to main content
GameDev.net gamedev.net
🔒 Locked

if statement problem

Started by JinJo Dec 3, 2004 at 8:32 AM 3 replies 400+ views
Original Post
JinJo
JinJo
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?
Trap
Trap
newline is '\n', not '/n'
JinJo
JinJo
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.
Enigma
Enigma
#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
JinJo
JinJo
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.