Going back to a previous line in a .txt file - c++

I've already read numerous articles on how to read a specific line from a .txt file, but none of them do what I need. I'm creating a simple parser for a sort of "programming language" and I want to include a "label" system. The thing is, though, in order to go to a specific label, I may have to go back or forwards in the text file. How do I go to a specific line in the .txt file? Is there a way to do this so I can return to the original line afterwards? Thanx in advance.

The best you can try is random access of files to move to any position of file.
Refer to : http://www.learncpp.com/cpp-tutorial/137-random-file-io/

Related

Possible to store data into text file permanently in c++

all I have a one small program in c++.I need to store some text in the .txt file it stored and file also created but again i run with some other data the previous data is deleted in .txt file, help anyone how to solve that problem i ask doubt is it possible in c++ yes/no.someone help!
The behavior you are describing is called overwriting a file.
You have two choices, if the file already exists:
Append to already existing file. Read more in How to append text to a text file in C++?
Write the data to a different file.

C++ Is it possible to read a specific line in a text file IF you have a marker of some sort somewhere in every line?

I was wondering if this can be accomplished using the ifstream library and using only a text file. Basically if I have multiple lines of information, regardless of whether or not they're the same length, would I be able to make my program read a specific line?
For example in the following file:
Bob Professor
Greg Singer Microphone Plane
Ed Runner Sun
If I wanted to read specifically line 2, aka the line starting with "Greg", is there a way to use markers to navigate to that line?
For example, is it possible to get to that line using the fact that it's the only one that starts with "Greg"? Or is there some kind of marker that I can place at some point in the lines to identify it? The desired result would be that so if I wanted to pull up a specific user's information from the text file (in this case, Greg), I'd be able to have the program read the information from that line.
Same thing if for example I wanted to pull up Ed, I'd just want it to read the line with Ed's info.
Any help would be greatly appreciated! Keep in mind, I can only use text files, so using another format is out of the question.

How to NOT OVERWRITE but put content in the middle of a file?

I am trying to write a c++ code that will put some additional text in the middle that not overwrite. I have tried every possible combination of tags but none of them are working. Can anybody give me an working example ?
For example :-
if input is :-
Hello!
Hey are you there ?
Is anybody home ?
Then the output should be :-
Hello!
Hey are you there ?
Where are you ?
Is anybody home ?
The Where are you text is inserted in the middle.I'm using c++ file handling.
I think that files work alot like arrays in that you can't just do an easy insert. For instance if you are implementing a vector or arraylist and want to insert a value in the middle you must shit all values after that. To insert in the middle I think you will need to shift all the contents bellow. I would maybe read everything into memory first or use a temp file.
This is not a limitation of C++ but of the underlying filesystem (on most modern file-systems).
A file is a block(s) of contiguous bytes, you can not append in the middle.
You have two options:
Read the file into memory.
Manipulate the file in memory
Overwrite the old file.
Open the file for reading and a temporary file for writing.
Copy from input file to output file until you get to the point you want to add text.
Write the modifications and finish the copy.
Replace the file with the tempfile.

Reading specific elements from a CSV file in C++

I'm trying to create a reference program which I think will use an excel spreadsheet to hold information for reading only. I want the user to be able to select a topic from an option list and have the information in the appropriate cell be fed back to them. The program is being written in C++. My question is, how do I access specific cells from a spreadsheet from my program? I've researched it a little and I've seen that I want to save my file as a csv and use fscanf to read the contents, but I'm at a loss as to how I would do this part. I googled it and found this thread:
http://www.daniweb.com/software-development/cpp/threads/204808/parsing-a-csv-file-separated-by-semicolons
but I think it reads in all of the data from the CSV? From what I can tell anyways. And I only want to pull specific elements. Is that possible?
If you only want specific elements, you would still have to parse all contents of the file until you reach those elements. You don't have to store values you don't need, but you do need to parse them to advance in the file.
Are you invoking the program from Excel? If you are, a little VBA goes a long way. You could always only export the cells of interest ready for your C++ program to read in.
Otherwise, other answers are correct. However, you don't need to load the entire file into memory at once. You can use std::fstream to open the file and read in each line of the file, parsing in the required information for each line.

Remove A Line Of Text With Filestreams (C++)

I have a large text file.
Each time my program runs, it needs to read in the first line, remove it, and put that data back into the bottom of the file.
Is there a way to accomplish this task without having to read in every part of the file?
It would be great to follow this example of pseudo code:
1. Open file stream for reading/writing
2. data = first line of file
3. remove first line from file <-- can I do this?
4. Close file stream
5. Open file stream for appending
6. write data to file
7. Close file stream
The reason I'm trying to avoid reading everything in is because the program runs at a specific time each day. I don't want the delay to be longer each time the file gets bigger.
All the solutions I've found require that the program process the whole file. If C++ filestreams are not able to accomplish this, I'm up for whatever alternative is quick and efficient for my C++ program to execute.
thanks.
The unfortunate truth is that no filesystem on a modern OS is designed to do this. The only way to remove something from the beginning of a file is to copy the contents to a new file, except for the first bit. There's simply no way to do precisely what you want to do.
But hopefully you can do a bit of redesign. Maybe each entry could be a record in a database -- then the reordering can be done very efficiently. Or perhaps the file could contain fixed-size records, and you could use a second file of indexes to specify record order, so that rearranging the file was just a matter of updating the indices.