reading from a file

so im just learning how to write in c++ and i have a question

// ces.cpp : Defines the entry point for the console application.
#include "stdafx.h" // use this line with Microsoft .NET
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream myfile ("number.txt");
if (myfile.is_open())
while ( myfile.good() )
getline (myfile,line);
cout << line << endl;

else cout << "Unable to open file ";

return 0;

i want to know how to read just one line from the file this is whats in the text file
for example i just want to read from the account 5551298

7221379 Abula 970.12
6853921 Allen 268.01
4215534 Austin 3821.94
5551298 Bailey 3193.97
6224649 Balovich 444.21
6502286 Bates 4253.79
8448936 Bettencourt 2916.99
2883903 Blackburn 906.31
6752376 Boucher 1291.99
3564698 Brown 2001.18
6373150 Duncan 2963.2
6375372 Estrada 1152.6
8736368 Fernandez 1113.05
7218536 Gallagher 3703.46
6681985 Gonzales 2667.88
3909086 Ha 3042.25
5221192 Hernandez 72.04
5901163 Howard 2229.66
2427821 Johnston 3130.92
6331655 Nguyen 3004.99
Last edited on
getline (myfile,line);

Looks like you got it right there.
but how do i get it to ask the user to into a account number?? and then have it display just that info because right now it just displays everything in the txt file
You can use the overloaded function
istream& getline (char* s, streamsize n, char delim )

Specify the delimiting character to be a space, after a space is found move to the next line. Do this until you find a match on the account number. When a match is found, display the current line using getline() without a specified delim (default is the newline character).
and where would i get the user to input the account number?
and where would i get the user to input the account number?
I take it you copied/were provided this program? What you're asking is one of the first things you should have learned. Forgive my lack of code tags, I'm on my phone:

std::string str;
cin >> str;

That has a variable called str, and cin extracts input from the keyboard until the first space and stores it into str.
You have 2 choices, maybe more but at least 2.

Get the string the user wants when they run the command, my favorite.
Or get the string after the program stats(Has limits in my opinion).

To get the string from the user when they run the command, I created a file named SearchFileForString.cpp.

I start main with
int main(int argc, char* argv[])

For my code I created 3 search patterns

    char *s0 = argv[0];          // Command
    char *s1 = argv[1];          // Search String
    char *s2 = argv[2];          // File to Search 

You do not really need s0 or argv[0] because that is just the command you ran such as SearchFileForString.

For my program I made argv[1] the string and argv[2] the filename so it's not hard coded, in other words you can use it for any file and any string.

Then I created a while loop to get line
and a check to see if the string was found
          if ((offset = line.find(argv[1], 0)) != string::npos)
          // If match found, Do this
          {// do nothing} 

This is a great program to write for beginners because you will learn a lot and once you have this working, you can modify it to do most anything with a file and string.
Line count, word count, char count, number count
single or double string searches
output to another file
sort contents
the list goes on and on.

Topic archived. No new replies allowed.