i'm a linux noob and i'm writing a program that needs to determine if a plain text file was created in linux or windows. i'm inclined to do something like:
then put the last character back into the stream and test it since the newline character is different between linux/windows, but i'm not really sure what i should be telling the program to test for (obviously not '\n') or if this is a bad way of going about it
sorry if this is an overly elementary question, but i didn't get an answer in beginners
There isn't really a way to do that. Sure, if you open the file in binary mode and check for '\n' (only then you will be able to tell the difference, '\n' automatically resolves to the systems default line break character(s) in text mode), you could tell what kind of newline characters it used. But there is technically no rule that a windows text program has to use \r\l as newline characters - though of course most will do that.
thanks. i'm probably going beyond what is required for the assignment, but i hate turning in code that could be broken even in obscure circumstances. maybe i'll rethink my algorithm and come up with one that won't have issues with windows v. linux files. if nothing else, i'll just see if he'll tell us which format the input file will be in
well, i'm kind of guessing that that's the problem. i can't find the thread i made in beginners with all the details and i'm about to board a flight, so here's a quick rundown
basically, i wrote all my code in windows, created a test file in windows. the very start of the program reads the input file into an array and prints it back out. it works fine when i compile the code in windows and use a .txt file created in windows. when i compile the code in ubuntu and use the same windows .txt file, i get (seemingly) random line breaks in the middle of the lines. if i recreate the .txt file in gedit and run the code with that, it runs as expected
void populateWorld(string file) {
ifstream fin(file.c_str()); //.c_str() because constructor needs c-string
//make sure the file opened
if (fin.fail()) {
cout << "The file was not found.nPress Enter to quit.";
getchar();
exit(0);
}
//determine the dimensions of the world and create new two-dimensional array
getRow(fin);
getColumn(fin);
allocateMemory();
//fill the world with the contents of the file
for(int i = 0; i < numRows; i++) {
for(int j = 0; j < numColumns; j++) {
world[i][j].status = fin.get();
}
fin.ignore(); //ignore the newline character
}
fin.close();
}
the weird thing is that right after these two function calls, main enters a loop that calls showWorld() at the end of the loop and the output is what's expected every time after the first. any ideas?
thanks. i think that code is a little beyond what i've learned thus far, but i'll check out that google search. i'm probably overthinking this, so i'll just check with my professor when i get back to school and see what he says about it.
The code just adds an extra condition if it is compiled on non-Windows machines, to strip the CR code from the end of the line of text.
On Windows, a line of text ends with '\r\n', and is read and translated to just '\n'.
On *nix, a line of text ends with '\n', so no translation occurs. When it reads a line of text from your Windows text file, it has an extra '\r' at the end of line.
I'm glad you are going to your professor for help. Good job!
It's not that at all. Too many people come here expecting us to do their work for them instead of putting out effort and using resources available to them.
In your case, however, you are just inexperienced and you are misunderstanding something simple. It otherwise looks like you are doing a good job!
Remember, when transferring text files between systems, use the TEXT mode. For everything else, use the BINARY mode. Your code shouldn't really have to do special stuff to handle improperly-formed text files.