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.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
usingnamespace 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!
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
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"
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