Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm making a program to read in a configuration file and I only want to read certain lines. Example:
config.txt:
This is a test configuration text file
It isn't supposed to read this line or the line above it
Read this line, but not the white space above or below it
Don't read this line or the white space above or below it
Read this line, but not the white space above or below it
I'm using your basic I/O:
FILE *File;
File = fopen("config.txt", "r");
You can mark lines you don't want to read with some character or a number, so whenever input stream reads it, you can skip it.
0 This is a test configuration text file
0 It isn't supposed to read this line or the line above it
1 Read this line, but not the white space above or below it
0 Don't read this line or the white space above or below it
0 Read this line, but not the white space above or below it
Then check value in first place of every line and skip it/ read it.
Or use something like multiline comments in C /* commented out */ check for opening character and skip everything before closing character.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
the task is to read an input file, store the content of the file in a string variable and perform some operations on it. I have a problem by removing all new line characters from the string. Reading the content of the file and storing it in a string variable works fine, but somehow trying to remove the new line characters doesn't work as it should.
For reading the content of the file, I just use an ifstream. The task recommends to misuse the string datatype in order to achieve fast input and output. This is done with the following lines, where file is the ifstream variable and size is the size of the file:
string buffer(size, ' ');
file.read(const_cast<char*>(buffer.data()), size);
This works perfectly fine, but if I try to remove the new line characters, the content of buffer changes somehow, see example below. I try to remove the new line characters with the following line:
buffer.erase(std::remove(buffer.begin(), buffer.end(), '\n'), buffer.end());
Example:
I have a text file with the content (no new line at the end):
line 1
line 78
line 3
I want the output to be:
line 1line 78line 3
Somehow the output is:
line 38
I really have no idea, why this is happening. Printing buffer after reading the file works perfectly fine, but after trying to remove the new line characters the output is always wrong.
I hope you can help me finding a solution.
Your file has Windows newlines \r\n. When you remove \n, you get line 1\rline 78\rline 3. \r in output is treated as caret return, i.e. move next output position to the beginning of the line. Thus line 78 rewrites line 1 and line 3 rewrites first 6 chars of line 78. You get line 38.
The simplest solution
buffer.erase(std::remove(buffer.begin(), buffer.end(), '\r'), buffer.end());
buffer.erase(std::remove(buffer.begin(), buffer.end(), '\n'), buffer.end());
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
how to write a cpp program for replace string using only 1 text file
for example if in text file abc.txt
Hello
how are you
good
bye
i want output like
Hello
how are you
bad
bye
all above thing will be done in text file
Pretty easy. Open the file, get the text. Then iterate throw each word and ask the user if he/she wants to change that word. If yes, write new word on new file. If not, write old word on new file.
Now you just need to translate that to c++
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have two text files. First one looks like this.
00000000000000000000000000000000
11100000000000000000000000000000
00010000000000000000000000000000
10100000000000000000000000000000
10100000000000000000000000000000
(the empty spaces in this file are a ' ' space character)
and the other one looks like this
11100000000000000000000000000000
00010000000000000000000000000000
10100000000000000000000000000000
10100000000000000000000000000000
00010000000000000000000000000000
i'd like to insert or replace the empty lines in the first text file with the second text file
The algorithm is pretty straightforward - it follows the general approach to two-way merging that you see in all algorithms:
Open both input files, and the output file, as streams
Read lines from the first file one-by-one
If the line that you read is non-empty, copy it into the output
Otherwise, read the next line from the second file, and copy it into the output
Once the first file is exhausted, copy the rest of the second file into the output.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm new to programming, so I was wondering...
If I have an input file consisting of 100 lines, how do I read only up to line 50 and print out each line?
Thanks.
create a fstream object fstream f("filename");
Keep a counter, read lines from file till the counter less than 50
Something like this
counter = 0;
while((counter < 50) && (f.good())
{
getline(f,str);
cout<<str<<endl;
counter++
}
Note: this is not the full code, but guideline how to do.
Please use 'fstream' to read your file and count each 'readline'. Each 'readline' means a full line which terminated with '\n'(return value without it). That should be useful.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I created a ofstream file.
How can I write to the beginning of current line on my file?
For example: I write:
a b c d e f
and now I want to add to the beginning the number of my letters (6) like this:
6 a b c d e f
You have to read the whole file in a byte array.
Then you write your "prefix" followed by you write the byte array to a tmp file.
Finally you have to delete the original file and rename the tmp file.
If you want to write at the beginning of an arbitrary line then you should read the whole file in an array of arrays of bytes, append your prefix to the line you want to edit and finally overwrite the original file.
HINT:-
If it is a text file then the best solution would be to flush the old contents into a temporary location, write what you need and append the old contents
Files are pretty static and don't support adding characters anywhere except at the end. If you need to add characters elsewhere, you need to rewrite the file. Also, files don't really have a concept of lines.
What you could do is recording the position of the file at the beginning of the line (using file.tellp()), write a couple of placeholders (e.g., spaces), and then the rest of the line. Once the line is complete, you'd reposition the write position (using file.seekp()) and overwrite some of the placeholders.
Personally, I wouldn't do anything like that! Instead, I would format the line into a std::ostringstream and, once completed write the line start information followed by the firmatted line (obtained from the std::ostringstream using str()). Well, ideally I'd write the information in one sequence directly to the file if it is readily available.
Files are essentially a stream of bytes that start at a specific location. The only way to insert new data in the front (or in the middle) of a file is to move the data that is after it. Since you are expecting to rewrite the first line, that would mean you would need to read the entire file, prepend your new data, and write out the entire (new) file over the existing one. You can do this with a single std::fstream object, but you will need to reset the file cursor to the beginning after you read the file. It would be more clear to read the file in using an std::ifstream object and then to overwrite the file with an std::ofstream object.
I have on my code:
file << args;
-->here I want to add to the beggining of this line a new argument.. (This argument has information of args But I must write args and after I have the information for the argument)
file << endl;