fstream for specific display

Does anyone know how to write a decent code using string and fstream to take an
and moving it to an "outputfile.txt" like this..

I just started using fstream but I don't even know where to start. If anyone would like to help me just understand how to break apart a line into separate integers and strings that'd be great.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void Process()
{
    std::ostream output("outrutfile.txt");
    std::ifstream input("inputfile.txt");
    if(!output.is_open() || !input.isopen())
        return; //Eror openong some file
    output << "City Total\n----------------------------" << std::endl;
    while(true) {
        std::string first;
        std::string second;
        std::string third;
        std::getline(input, first, ',');
        std::getline(input, first, ',');
        std::getline(input, first);
        if (input.fail())
            break; //End of input or malformed file
        double total(std::stod(first) + std::stod(third));
        //Now you have both city name (in second) and total (in total)
        //Output it here
    }
}

std::stod() — converts string to double
std::getline(input, first, ','); — reads characters from input to first until it encounters ','
Last edited on
Better to use a stringstream to parse each line;
that way, one badly formed line would not mess up everything that follows.

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
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <sstream>
 
void print_city_totals( std::istream& input, std::ostream& output )
{
    constexpr char COMMA = ',' ;
 
    std::string line ;
    while( std::getline( input, line ) ) // for each line input the input stream
    {
        std::istringstream stm(line) ;
 
        double first ;
        stm >> first ; // get the first number
 
        stm.ignore( 100, COMMA ) ; // throw away the comma
 
        std::string city_name ;
        std::getline( stm, city_name, ',' ) ; // read everything upto the comma into name
 
        double second ;
        stm >> second ; // get thesecond number
 
        if(stm) output << city_name << ' ' << first+second << '\n' ;
        else output << "[ error in line: '" << line << "' ]\n" ;
    }
}
 
int main()
{
    std::istringstream input( "15.1,Chico,17.4\n"
                              "34.4,Seattle,30.52\n"
                              "what is this?\n"
                              "2.9,Portland,26.1\n"
                              ".5,Death Valley,1.1\n" ) ;
 
    print_city_totals( input, std::cout ) ;
}


http://ideone.com/GFx61i
Thank you so much JL and mini.
Last edited on
You can use std::setw() manipulator: http://cplusplus.com/reference/iomanip/setw/?kw=setw
Is there a better way to call stod? Because I keep getting an error saying 'stod' is not a member of 'std'.
Yes, std::stod or have using std::stod; somewhere near the top of the file.
In both cases it does not recognize stod. Can anyone help?
Did you forget to include the string library?
#include <string>
Yes I have string, I tried having using std::stod, and having stod in the double function like this.
double total(std::stod(first)+std::stod(third));
Both time is says 'stod' is not a member function of 'std'
Last edited on
Looks like you got an outdated compiler which isn't supporting C++11. Best way is to updadte it. Also you can use std::atod(first.c_string());

1
2
ifstream fin("inputfile.txt");
fin.open("inputfile.txt");
Remove second line. You are already opening file in constructor. And I do not know what will happens if you try to open same file again. (I hope it closes it properly before opening again)
Topic archived. No new replies allowed.