Read a text file and Loop through each letter or number in the line

Hello, I just started learning to program and i need some help. I've tried searching but I don’t understand any of the answers.
So I’m given a .txt file that looks like this
E123456675786897907987867567467
F4545465876897980980
J54564676987979890879786756456455634
..................................................................
I'm asked to make C++ code to
-Open the text file
-Loop through the lines
-Loop through each letter or number in the line (i.e., read it as a string)
-Open an excel sheet
-Output the results to the excel sheet
I can get C++ to read the file & output the file in excel sheet; but I don’t get how to Loop through each letter or number in the line (i.e., read it as a string); if that’s even possible. Anyways, if someone could help me with an answer that would really help. Thanks.
but I don’t get how to Loop through each letter or number in the line (i.e., read it as a string); if that’s even possible.


What do you mean ? You can only read a std::string or char[] from a file. The numbers, by default would be read as std::string or char[].
Here is a loop:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <fstream>

std::ifstream ifs("my-data.txt");

std::string line;

while(std::getline(ifs, line))
{
    // do whatever you want with the line here
}
Last edited on
I have done this loop but it is not working. it just reads only one line of the text file and then it stops.

while(std::getline(ifs, line))
{
int i=0;
intReturn = atoi(line.c_str());
word=line.substr(i,intReturn);
i=i+intReturn;
}
Please help.
intReturn = atoi(line.c_str());


Do you need to that ? You only got to put it in an excel file. So, why do you need to convert it to number ? Try putting all numbers in a string array as strings.
I have done this loop but it is not working. it just reads only one line of the text file and then it stops.


I think you may be causing a SEGFAULT with your substr() call.

The question says this:

Question wrote:

-Loop through each letter or number in the line (i.e., read it as a string)


You can do that like this:

1
2
3
4
5
6
7
8

for(int i = 0; i < line.length(); ++i)
{
    char ch = line[i]; // select one letter/number at a time from string line.

    // do whatever you like with your letter/number stored in ch.
}

Actually i want to read lines from a text file.then write each line in a row and each character in a column of csv file. the text file has 600 lines. Bellow i am giving some of that lines

W39000779310701010008085018904037037064114044012044
W39000779310701010008089017204038037061112035012038
W39000779710701010009087022905047042057013055097033012037
W39000779310701010008097004304010030008144014035011
W39000779310701010008101002504011035010059002009002
W39000779310701010005097007702030068047
in my code its only take the first line and then stop working. please help me on this.

Thanks
Last edited on
I have already given you working answers to your questions. I'm not going to write the program for you.
you are not getting my point. i have done what you have suggested, but still it is running only for one line. then it says error occurred while running the program.
What do I do now?
Why dont you just post the line that causes the error ?
Yes, please post the code exactly as you have it now so we can check what you have done and see what might be causing it.
Last edited on
Topic archived. No new replies allowed.