Can someone help me with File I/O

I have a file with 100+ lines.

I want to check each line one by one. After I am done with Line 1 I move on the Line 2.

Okay I think the real question is:
How do I know if it is at the end of Line 1, line 2 and etc.
Last edited on
What have you written so far? If you just post a question very few people will help you. Its better to attempt writing it and when you hit an error then present it to the forums.

Ill help with some psuedo code so you will be headed in the right direction.
First you want to open your file. If you want to read the entire file use a while loop. If you want to read x amount of lines use a for loop with either the program or user limiting the number of lines read. You can use getline() to read each line in the file everytime its called. Hence the for or while loop depending on the number of lines you want to read.

heres a link: https://stackoverflow.com/questions/39203338/reading-from-file-line-by-line
File name - example.txt
This is a line.
This is another line.

I would like to ONLY print line 2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;

int main () {
	ifstream i("example.txt");
	
	if(!i)
	{
		exit(EXIT_FAILURE);
	}
	
	string line;
	vector<string> vline;
	int r = 0;
	while (i.good())
	{
		getline(i,line);
		vline.push_back(line);
		cout << vline[r] << endl;
		vline.clear();
	}
	
  return 0;
}


You can give me some psuedo code, I don't need anyone to finish my problem. I know what I need to do but I just don't know how to implement it using c++. I don't know if there is a pointer where it reads character by characters. I just want to know how I can know if it is at the end of the line. How do I know if the program is at the end of the first line.

I tried using a counter. But it always prints the first line!
Last edited on
1
2
3
4
5
6
7
8
9
10
	while (i.good())
	{
		getline(i,line);
		vline.push_back(line);
	}
	
	//cout << vline[0] << endl;
	cout << vline[1] << endl;
	cout << vline[2] << endl;
	


Now the question is I have hundreds of lines.
Wait...

1
2
3
4
5
//Put all lines into the vector
for(int j = 0;j<v.size();j++)
{
      //compare
}
Why are you doing it on the vector size? Are you putting everything into a vector when you read it? Ill give you an example. Its in C so it wont be the exact answer youre looking for but should be close enough where you can figure out how to implement it into your code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char fileName[100];
        strcpy(fileName, argv[1]);
        FILE *fPtr;
        if ((fPtr = fopen(fileName, "r")) == NULL){
            printf("Error! opening file");
        }
        else
        {
            //Holds line from file
            char fileLine[150];
            for(int i = 0; i < 10; i++)
            {
                fgets(fileLine, sizeof fileLine, fPtr);
                fputs(fileLine, stdout);
            }
        }
        fclose(fPtr);


In my program it reads the line and immediately displays it. It doesnt store whats in the file just reads it from file and displays it
Last edited on
Also to answer your question typically the end of a line ends with a newline character which is usually \n

so an example would be

std::cout << "Hello \n World";



this would display
hello
world
Last edited on
Thank you KingKush I have another problem

In this situation
1
2
3
4
5
6
7
8
9
10
while (i.good())
	{
		i >> line;
		vline.push_back(line);
	}
	
	//cout << vline[0] << endl;
	cout << vline[1] << endl;
	cout << vline[2] << endl;
	


v[0] holds "this"
How do I print i from the first cell?

I am not too sure if I can put all the characters of "this" and put it into a character vector.
Since youre using c++ ignore the character array from my code and instead create a string vector. I dont think the newline character gets saved in vectors tho so after every pushback of a new line element also do a push back for \n. (double check this tho)

Can you post your text file? So i could see the layout

If your vector is a of type char then the vector is storing [T][H][I][S]. so vector[2] will display the 'i' in "this"
Last edited on
the istream ">>" operator splits on whitespace by default (space, tab, newline, etc.). It's perfect for files with a consistent formatting, but might not work for your use-case of extracting whole lines at a time (which might have spaces!)

if your file looked like this
A 1
B 2
CCC 32
D 40
EEEE 5


Then you could neatly use that operator and extract all your data:
1
2
3
4
5
6
7
8
9
10
ifstream ifs("myfile.txt");
vector<string> words;
vector<int> nums;
string word;
int n;
while (ifs >> word >> n)
{
    words.push_back(word);
    nums.push_back(n);
}


Lines don't particularly matter (it could all have been on one space-separated line), as long as the pattern continues with <word><whitespace><number><whitespace>....

In your case, you'd probably want a solution involving getline
Topic archived. No new replies allowed.