HELP w/ I/O Streams

I seem to be missing a concept or 2 here ... I am tasked with writing a program that reads text from a file and outputs each line to the screen as well as to another file PRECEDED by a line number ...

in addition, I have to Print the line number at the start of the line and right-adjusted in a field of 3 spaces ...

Follow the line number with a colon then 1 space, then the text of the line.

Another kicker, is I have to grab the data 1 character at a time and write code to ignore leading blanks on each line.

Here is what I have so far:

#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cctype>

using namespace std;

int main()
{
char next;
int count = 0;

ifstream fin;
fin.open("myFile.txt");

ofstream fout;
fout.open("newFile.txt");

if(fin.fail( ))
{
cout << "Input file opening failed. \n";
system ("pause");
exit(1); // exit the program
}
cout << ++count << ": ";
fout << count << ": ";

//read in the lines one character at a time
while(!fin.eof() && fin.get(next))//until the end of the file is reached
{
if(next == '\n') //Problems seem to begin at this point
{
fin.get(next);
cout << ++count << ": " << next;
fout << count << ": " << next;

if(!isalpha(next))
{
fin.get(next);
}
else
{
cout << ++count << ": " << next;
fout << count << ": " << next;
}

}

cout << next;
fout << next;
}

fin.close();
fout.close();

getch();
return 0;
}

Any help would be greatly appreciated.

closed account (GbX36Up4)
I would be willing if you formatted your code.
formatted???
To read a line, use std::getline()
http://www.cplusplus.com/reference/string/string/getline/

1
2
3
4
5
6
std::ifstream fin( "myFile.txt" ) ;
std::string line ;
while( std::getline( fin, line ) )
{
     // do something with the line
}



To print a number right-justified in a field of 3 spaces, use std::setw()
http://en.cppreference.com/w/cpp/io/manip/setw

1
2
3
4
5
for( int i = 0 ; i < 20 ; ++i )
{
     static int line_number = 0 ;
     std::cout << std::setw(3) << ++line_number << '\n' ; // #include <iomanip>
}



To ignore leading blanks on each line use std::string::find_first_not_of()
http://www.cplusplus.com/reference/string/string/find_first_not_of/
and std::string::substr()
http://www.cplusplus.com/reference/string/string/substr/

1
2
3
4
std::string line = "      hello world" ;
std::string::size_type pos = line.find_first_not_of( ' ' ) ;
if( pos == std::string::npos ) pos = 0 ;
std::string line_with_no_leading_space = line.substr(pos) ;
I initially coded using "std::getline()", but then I noticed that I had to grab 1 character at a time ... the other stuff sounds good.

Thanks!
Couldn't you use getline and then iterate through the string to retrieve each charater?
Honestly, I'm not sure ... I had the impression that I couldn't.

This program really aggravated me ... it seemed so easy, but I simply experienced an epic fail.
> This program really aggravated me ... it seemed so easy, but I simply experienced an epic fail.

Do it step by step, one small step at a time, testing each step to make sure that it is working before you move on to the next step, and you would find that it is easy:

Step1 : read each line of text from a file and print the line to the screen
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    const char* const path_to_file = __FILE__ ; // or "abcd.txt" or whatever
    std::ifstream file(path_to_file) ;
    std::string line ;
    while( std::getline( file, line ) ) std::cout << line << '\n' ;
}


Step2: also print each line to another file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    const char* const path_to_input_file = __FILE__ ; // or "abcd.txt" or whatever
    const char* const path_to_output_file = "output.txt" ;

    std::ifstream fin(path_to_input_file) ;
    std::ofstream fout(path_to_output_file) ;

    std::string line ;
    while( std::getline( fin, line ) )
    {
        std::cout << line << '\n' ;
        fout << line << '\n' ;
    }
}


Step3: To another file PRECEDED by a line number right-justified in a field of 3 spaces followed by a colon then 1 space, then the text of the line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

int main()
{
    const char* const path_to_input_file = __FILE__ ; // or "abcd.txt" or whatever
    const char* const path_to_output_file = "output.txt" ;

    std::ifstream fin(path_to_input_file) ;
    std::ofstream fout(path_to_output_file) ;

    std::string line ;
    while( std::getline( fin, line ) )
    {
        std::cout << line << '\n' ;

        static int line_number = 0 ;
        fout << std::setw(3) << ++line_number << ": " << line << '\n' ;
    }
}


Step4: Also, ignore leading blanks on each line.
(For variety, we will use Yoni Revah's idea: iterate through the string to retrieve each charater)
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
26
27
28
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cctype>

int main()
{
    const char* const path_to_input_file = __FILE__ ; // or "abcd.txt" or whatever
    const char* const path_to_output_file = "output.txt" ;

    std::ifstream fin(path_to_input_file) ;
    std::ofstream fout(path_to_output_file) ;

    std::string line ;
    while( std::getline( fin, line ) )
    {
        // strip leading white space: iterate till we get a non-white-space character
        std::size_t pos = 0 ;
        for( char c : line ) if( std::isspace(c) ) ++pos ; else break ;
        line = line.substr(pos) ;

        std::cout << line << '\n' ;

        static int line_number = 0 ;
        fout << std::setw(3) << ++line_number << ": " << line << '\n' ;
    }
}

Topic archived. No new replies allowed.