Text file help

Pages: 12
I've created a text file with the numbers from 1-450. After writing code to retrieve and print out the contents in the text file, the compiler only printed out the numbers 124-450. Is there a reason why this is happening?
closed account (Dy7SLyTq)
we could guess all day, or you could [voice="gameshow_host"] SHOW US THE CODE!!!!!!!!!!!!!!!!!!![/voice]
int main()
{
string line;
vector <string> text;
ifstream file ("nums.txt"); /// Calls the text file
if (file.is_open()) /// Check if file is open
{
while(file.good()) ///Check whether the file is valid or corrupt
{
getline(file, line); /// Get lines from text file
text.push_back(line);
}
for (int i = 0; i <= text; i++)
{
cout << text[i] << endl;
}
file.close();
} /// if & while loops open and shows the data in the text file.
return 0;


=====================================================
And this is the text file:
1
2
3
4
5
.
.
.
450
Last edited on
closed account (N36fSL3A)
What's the point of get line if you have one value on a line?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
    std::vector<std::string> text;
    std::string buffer;
    std::ifstream file("nums.txt");
    if(file)
    {
        if(file.good())
        {
            for(int i = 0; i < 450; i++)
            {
                ifstream >> buffer;
                text.push_back(buffer);
            }
        }
    }

    for(int i = 0; i < 450; i++)
    {
        std::cout << text[i];
    }
    
    return 0;
}
Last edited on by Fredbill30
closed account (Dy7SLyTq)
because its easy to just grab the line which is his goal...
closed account (N36fSL3A)
He has one value on each line. Each line has a separate space in the vector. Getline is unnecessary.
closed account (Dy7SLyTq)
no its not. its what i would do. its not unneccasary. its safer than cin>> buffer imo
closed account (N36fSL3A)
Not in this case...
closed account (Dy7SLyTq)
yes it is.
closed account (N36fSL3A)
How?
closed account (Dy7SLyTq)
lets say that the current file has/is
a) a space between two numbers on one line (as an error)
b) just a test for future files that he will have to do some operation on each line.

if he does cin he has to remember to change it if he needs to grab each line later. there are others but ive almost finished my first pandarian ;)
closed account (N36fSL3A)
But it doesn't. If it did, I'd recommend it.
closed account (Dy7SLyTq)
how do you know that? as i said the current file could be a test. its incredibly static. getline allows more functionality
closed account (N36fSL3A)
Okay. Getline is really only useful for getting full strings. This doesn't require it.
My question is, is there a problem with my code that's making the compiler not read all of the numbers and only reading 124 through 450?
closed account (Dy7SLyTq)
oh duh in the for loop make it text.size();
closed account (N36fSL3A)
Makes more sense^. Only for the last loop tho.
Last edited on by Fredbill30
I did that and it still doesn't work.
closed account (N36fSL3A)
did what? My example? Try to erase the file.good() thing.
Last edited on by Fredbill30
^ I tried your code and it works. But I need each number to be on a separate line. I modified std::cout << text[i]; and changed it to std::cout << text[i] << endl; and encountered my same issue.
Pages: 12