reading file line by line

my code is not able to read files line by line , i am not bale to figure out whats wrong pls help

1
2
3
4
5
6
7
8
9
10
11
12
13
  #include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main() {
   ifstream input("20111129.txt");
  for( std::string a; getline( input, a ); )
	{
		cout<<a<<endl;
	}
    return 0;
}
Most likely reason is that the file is not in the current working directory when you run the program.
File not found => nothing to read.
its on desktop still not getting required outputs
And is the desktop the working directory when you run the program? How are you ensuring that?
yes
Well, that's one of my two questions you answered. Any reason why you completely ignored the other one?
i tried reading integers and it works, but it cannot read line by line from the file and does not give output
Last edited on
is my code wrong ??
Code looks ok.

You say "works". Did you read integers from a file?
yes
You say that something like this "works", assuming that the 20111129.txt has integers in it:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <fstream>

int main() {
   std::ifstream input( "20111129.txt" );
   for ( int a; input >> a; )
   {
      std::cout << a << '\n';
   }
}
Hello mnnsa,

Yes the code does have some problems.

You are missing the header file "string" which "getline" needs and also if you are using a "std::string" you will need this header file.

Line 7 defines a file stream and tries to open the file, but how do you know it is open?

You did not bother to mention what IDE you are using which makes it hard to tell where the current working directory is. This could also change depending on how and where you run the program from.

If you are using an input file please share it, or at least a good sample, so everyone can use the same information. This could also illuminate other problems with your code.

I changed the variable names. Try not to use single letter names as this is more confusing than helpful.

I have changed your code a bit and added some blank lines to make it easier to read. The blank lines help. See what you think.

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>  // <--- Added.

using namespace std;

int main()
{
    ifstream inFile("20111129.txt");

    if (!inFile)  // <--- Added.
    {
        std::cout << "\n File \"20111129.txt\" did not open" << std::endl;

        return 1;
    }

    for (std::string line; getline(inFile, line); )
    {
        cout << line << endl;
    }

    return 0;
}


Andy
thank u got this,
but one last query
if i want to run this program for all files in a folder how can i do this ?
Hello mnnsa,

My question would be. Do you need to get the file names from the directory or do you know them already?

After that you would have to rework the code into a loop to deal with each file name. The rest of the changes would not be that hard to change.

Andy
Topic archived. No new replies allowed.