How to jump to the end of a line in a text file

Pages: 12
closed account (y8h7M4Gy)
Hello, I am making a level editor for the game I'm working on!

I am using SDL and C++

My problem is... I want to make it so you can resize the level after you already made the map... why this is an issue is because the maps are saved like so.

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


Is there any command that only jumps to the end of ONE line? because ive been looking for one and it kept saying eof, but that jumps to the end of the whole file.

thanks in advance!
Last edited on
No.

You can read an entire line (with getline), which will move you position to the next file.

If your file was binary, you could have random access to the file (rather than sequential) with seek().
closed account (y8h7M4Gy)
can i use getline to go to the end of the line and then insert more numbers? also why cant i use seek with non-binary?
You can not go to the end of a line in a file and simply add a new object at the end of it... this would overwrite the next few bytes (depending on the sice of the new one) instead of adding it...

The most fitting practice would be to read the whole file into a buffer (or multiple buffers - one for each existing line -) and add the data there.
Then write the buffer´s content(s) back to the file...
closed account (y8h7M4Gy)
so you mean like counting each line as a different string and then saving each string as a different line?
Why don't you just store the map's width and height somewhere and use seekp() to move the put pointer to the proper position?
Why don't you just store the map's width and height somewhere and use seekp() to move the put pointer to the proper position?

That still wouldn't get around the problem Incubbus mentioned.
When you change the map width, you'll have to rewrite the entire file, binary or not.
You can read a whole line into a std::string by using the std::getline() function (not cin.getline())
I think the easiest way to do this is reading in your file, line by line and outputting the modified line to a different file.
closed account (y8h7M4Gy)
Hmmm... ok, i can use std::getline to do that right?
Right!
1
2
3
4
5
6
7
8
9
std::ifstream ifs("infile.txt");
std::ofstream ofs("outfile.txt");

std::string line;
while(std::getline(ifs, line))
{
    // fix line here
    ofs << line << '\n'; // output adjusted line
}
closed account (y8h7M4Gy)
ok thanks, but one problem... .txt??? who uses txt's?? windows users i tell you haha
who uses txt's??

You are.
closed account (y8h7M4Gy)
im talking about the extension, obliviously i use text files tho lol
What's so funny about Windows users?
closed account (y8h7M4Gy)
I dunno, i use Windows from time to time myself, i just prefer Linux Arch or Ubuntu
I dunno, i use Windows from time to time myself, i just prefer Linux Arch or Ubuntu

The .txt extension is commonly used on all systems to mark plain text files as such.
closed account (y8h7M4Gy)
i know, but with Linux it automatically detects it anyway
closed account (y8h7M4Gy)
oh and i don't have anything against windows, i just don't like programming on it.

MAC = Entertainment
Linux = Programming
Windows = Games

;) thats what i use them for.. except i don't use mac unless i steal a friends' laptop haha
edward wrote:

MAC = Entertainment
Linux = Programming
Windows = Games

That's ridiculous. All of those operating systems are general-purpose. You can play games on a Mac or on Linux (Steam and the Source engine were ported to Mac recently, and there's also WINE), you can program on a Mac or on Windows and you can do "entertainment" on Linux and Windows.

edward wrote:
i don't have anything against windows
edward wrote:
windows users i tell you haha

Well clearly, you assume all it's users are idiots...

i know, but with Linux it automatically detects it anyway

That's nothing to do with Linux. That's the file manager you're using. And it doesn't "automatically detect" anything. It just checks the file's mime-type (which I assume is stored somewhere) and whether or not it's executable to see what kind of file it is, what image to display as an icon for it, and what to do when the user tries to open it.
Pages: 12