c++ file read and write syntax

Can anyone help me with C++ syntax please.

I need to read in a file created in Notepad with 25 rows
On each row there is a mixture of 8 variables (strings, integers, etc).

I'm OK with the manipulation of the data once I have it, but I then have to write it back out to
a file (comma delimited) so that Excel can produce graphs with it.

I don't know the syntax for reading in such a file to the correctly defined variables
or for writing back out to comma delimited files.
Last edited on
I have tried this but ifstream and getline reads in the whole 25 rows as one big string.
I want to be able to populate different variable types. The program also seems to stop if
it encounters a space when reading in.

The file looks like this:

Fridge,420,24,0000,2400,1,1,y
Washing machine,512,4,0800,2200,2,2,n
Dishwasher,1200,2,0800,2200,2,2,n
Microwave,1450,1,0800,2200,5,5,n
Oven,2300,2,0800,2200,1,1,n
Toaster,1550,0.2,0600,0000,3,4,n
kettle,1900,1,0900,1800,5,5,n
Tumble Dryer,2790,2,1000,1700,1,1,n
Freezer,700,24,0000,2400,1,1,y
Television,300,2,0700,2400,2,4,n
PC,240,2,0800,2400,3,2,n
Games Console,120,2,1200,2400,1,3,n
Sound System,150,1,1800,0000,1,2,n
DVD Player,40,1,1800,2400,1,2,n
Central Heating,4474,12,0000,2400,1,1,n
Telephone,19,24,0000,2400,1,1,y
Broadband Router,12,24,0000,2400,1,1,y
Lighting morning,950,2,0700,0900,1,1,n
Lighting evening,950,4,1900,2300,1,1,n
SHower,240,2,0600,2200,1,1,n
Electric Shaver,15,0.1,0600,1000,1,1,n
Hair Dryer,1000,0.1,0600,1000,1,1,n
Straightners,120,0.1,0600,1000,1,1,n
Mobile Phone Charger,5,2,0000,2400,1,1,n
Iron,1000,0.5,0700,1000,1,1,n
have tried this but ifstream and getline reads in the whole 25 rows as one big string.

No it doesn't. If i create a text file and and put the firt too lines of your file into it and then run this program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::ifstream myfile("myfile.txt");
    std::string data;
    
    while(! myfile.eof()) {
            getline(myfile, data);
            std::cout << data << "\n\n";
    }
    
    std::cin.get();
    return 0;
}


It runs just as expected. The first line "Fridge,420,24,0000,2400,1,1,y" is outputted, followed by a gap, followed by "Washing machine,512,4,0800,2200,2,2,n".
Excellent this is reading the individual rows now.

But the string data will always be a diffent length. So how do I put it into different
variable types? I was hoping to read them in from the file to the different variable types. eg
string for the appliance description, integer for duration, etc....
It depends if the data is always going to be in the same format or not. You could use getline and use a delimter of ',' to stop reading at that point in the row. Then, convert it to a number if it is a number or leave it as a string if it isn't. I personally would probably continue reading one row at a time, then use a function to break up that line into the seperate values, then convert them etc...
Topic archived. No new replies allowed.