Need help with file i/o program

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1417 Views - Last Post: 18 April 2012 - 07:57 PM Rate Topic: -----

#1 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Need help with file i/o program

Posted 17 April 2012 - 12:23 PM

Here's a snapshot what i'm trying to do, but right now i'm just trying to take the first name and last name from a file and format by last,first.
Here's my code so far, but it's not pulling any data from my file at all.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

/*
 * 
 */
int main() 
{
    //declare variables
    string firstname;
    string lastname;
    string reading;
    string systolic;
    string diastolic;
    int loopcount;
    ifstream patient;
    string line;
    
    
    //open file
    patient.open("patient.open");
    
    patient >> line;
    
    firstname = line.substr(0,8);
    
    lastname = line.substr(8,12);
    
    systolic = line.substr(14,16);
    
    diastolic = line.substr(17,19);
    
    //print name to screen
    cout << lastname;
    cout << ", " << firstname;
    
    
    

    return 0;
}
The attached file shows what i'm trying to accomplish by the end of my program

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with file i/o program

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Need help with file i/o program

Posted 17 April 2012 - 12:33 PM

You do know that the extraction operator>> stops when it encounters a white space character, correct? Try printing your variable line and see what it contains.

Also you should always insure that your file opens correctly.

Jim
Was This Post Helpful? 2
  • +
  • -

#3 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 280
  • View blog
  • Posts: 1,787
  • Joined: 20-September 08

Re: Need help with file i/o program

Posted 17 April 2012 - 02:17 PM

Further to what Jim said, you may also use the fact that ...
cin >> intVar; // extracts 110 in this "     110/75" stretch of char's


Note also, that your file seems to have the last name in the first position, so you would read/extract the last name at first.

struct BP // you could define a data struct named BP
{
   string last;
   string first;
   int sys;
   int dys;
} ; // Note ';' char to tell compiler end of struct

void processFile( const char* FNAME )
{
   ifstream fin( FNAME ); // open this file for input as an ifstream obj called fin
   if( fin ) // if opened ok ...
   {
      BP tmp; // construct empty BP obj
      char dummy;
      while( fin >> tmp.last && fin >> tmp.first &&
             fin >> tmp.sys && fin >> dummy // eat '/' char ...
             && fin >> tmp.dys )
      {
         // process this data struct ...
         cout << tmp.last << ", " /* ... */ << endl;
         fin.ignore(); // eat newline char at end of line
      }
      fin.close(); // since done with file, can close it now ...
   }
}


This post has been edited by David W: 17 April 2012 - 02:22 PM

Was This Post Helpful? 1
  • +
  • -

#4 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Re: Need help with file i/o program

Posted 17 April 2012 - 04:26 PM

View PostDavid W, on 17 April 2012 - 02:17 PM, said:

Further to what Jim said, you may also use the fact that ...
cin >> intVar; // extracts 110 in this "     110/75" stretch of char's


Note also, that your file seems to have the last name in the first position, so you would read/extract the last name at first.

struct BP // you could define a data struct named BP
{
   string last;
   string first;
   int sys;
   int dys;
} ; // Note ';' char to tell compiler end of struct

void processFile( const char* FNAME )
{
   ifstream fin( FNAME ); // open this file for input as an ifstream obj called fin
   if( fin ) // if opened ok ...
   {
      BP tmp; // construct empty BP obj
      char dummy;
      while( fin >> tmp.last && fin >> tmp.first &&
             fin >> tmp.sys && fin >> dummy // eat '/' char ...
             && fin >> tmp.dys )
      {
         // process this data struct ...
         cout << tmp.last << ", " /* ... */ << endl;
         fin.ignore(); // eat newline char at end of line
      }
      fin.close(); // since done with file, can close it now ...
   }
}


wow you seem like you know a lot more than what i have learned so far. This is my first semester of programming and i'm very amaetur. I'm having a hard time understanding everything that the code you wrote does even with the notes. But i do appreciate any help your offering to give. What exactly is a data struct like you have up above named BP?
I got the code to print my last name, first name by using the cout by using a substring, but when i start to print the systolic and diastolic readings my program prints them to the screen in a weird order. For example if i have my code set up like this
//declare variables
    string lastname;
    string firstname;
    string reading;
    string systolic;
    string diastolic;
    int loopcount;
    ifstream patient;
    string line;
    
    
    //open file
    patient.open("patient.dat");
    
    patient >> line;
    
    firstname = line.substr(0,7);
    lastname = line.substr(7,10);
    systolic = line.substr(11,14);
    diastolic = line.substr(16,17);
    
    cout << lastname;
    cout << "," << firstname;
    
    cout << systolic;
    
    cout << "/"<<diastolic;


it prints lastname systolic/diastolic, firstname systolic/diastolic/diastolic

I have my patient.dat file set up exactly like this:
HoustonRash115/75
Was This Post Helpful? 0
  • +
  • -

#5 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Re: Need help with file i/o program

Posted 17 April 2012 - 04:32 PM

View Postjimblumberg, on 17 April 2012 - 12:33 PM, said:

You do know that the extraction operator>> stops when it encounters a white space character, correct? Try printing your variable line and see what it contains.

Also you should always insure that your file opens correctly.

Jim


I did print just the line and it opened the entire file exactly how it was. But then i tried to extract just the last name portion from the line "HoustonRash115/75"
and it printed "Rash115/75". when i put a space between the name and the data reading it only printed "Rash".
If i keep that space in the .dat file then when i try to print the data readings they won't show up to the screen using a substr.

Can you explain why if you know?

//declare variables
    string lastname;
    string firstname;
    string reading;
    string systolic;
    string diastolic;
    int loopcount;
    ifstream patient;
    string line;
    
    
    //open file
    patient.open("patient.dat");
    
    patient >> line;
    
    lastname = line.substr(7,10);
    cout << lastname;
    
    firstname = line.substr(0,7);
    cout <<","<< firstname;
    
    systolic = line.substr(11,13);
    cout << systolic;


Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Need help with file i/o program

Posted 17 April 2012 - 05:29 PM

Quote

If i keep that space in the .dat file then when i try to print the data readings they won't show up to the screen using a substr.

That's because when you use the extraction operator>> it stops processing when it encounters a space. So the line Jones Tom 200/78 you would only retrieve Jones with a single extraction operator. This is actually very good because that is exactly what you want to happen. Extract the last name, then extract the first name with another extraction. To extract the blood pressure readings into a numeric variable you can use the extraction operator>> to extract the first number, extract a single character to retrieve the '/' character and the extract the next number. This will work because when you try to retrieve a number the extraction operator>> stops when it sees a whitespace character and when it sees a non-numeric character, in this case the backslash '/'. You won't need the substring functions at all, you will do the entire processing using the extraction operator.

Instead of using strings for the systolic and diastolic variables you could use an int type. See this simple tutorial for file input and output. After you read the tutorial give your program another try. You will need 5 variables.

string last_name;
string first_name;
char trash; // used to retrieve the backslash character.
int systolic;
int diastolic;



Jim

This post has been edited by jimblumberg: 17 April 2012 - 05:30 PM

Was This Post Helpful? 2
  • +
  • -

#7 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Re: Need help with file i/o program

Posted 17 April 2012 - 06:27 PM

View Postjimblumberg, on 17 April 2012 - 05:29 PM, said:

Quote

If i keep that space in the .dat file then when i try to print the data readings they won't show up to the screen using a substr.

That's because when you use the extraction operator>> it stops processing when it encounters a space. So the line Jones Tom 200/78 you would only retrieve Jones with a single extraction operator. This is actually very good because that is exactly what you want to happen. Extract the last name, then extract the first name with another extraction. To extract the blood pressure readings into a numeric variable you can use the extraction operator>> to extract the first number, extract a single character to retrieve the '/' character and the extract the next number. This will work because when you try to retrieve a number the extraction operator>> stops when it sees a whitespace character and when it sees a non-numeric character, in this case the backslash '/'. You won't need the substring functions at all, you will do the entire processing using the extraction operator.

Instead of using strings for the systolic and diastolic variables you could use an int type. See this simple tutorial for file input and output. After you read the tutorial give your program another try. You will need 5 variables.

string last_name;
string first_name;
char trash; // used to retrieve the backslash character.
int systolic;
int diastolic;



Jim

thank you that tutorial is very helpful,but i'm still not understanding how to "just" extract the the letters from the numbers if i'm using strings for both first name and last name.
I tried to use the ignore function to get past my first name in the .dat file and it worked, however, it printed the numbers after the last name as well. I know it's doing what i'm telling it to do, but how do i get it to stop reading after the last name has been read?
int main() 
{
    //declare variables
    string lastname;
    string firstname;
    char trash;
    int sys;
    int dia;
    ifstream patient;
    
    
    
    //open file
    patient.open("patient.dat");
    
    patient.ignore(7,);
    patient>>lastname;
    cout<<lastname;
    
    
   

    return 0;
}



Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Need help with file i/o program

Posted 17 April 2012 - 06:34 PM

Why are you trying to ignore something. Don't you need all the information?

You have a file that contains the following infomation:
last_name first_name systolic_number/diastolic_number

So use the extraction operator>> to extract the last_name into your lastname variable, then use another extraction operation to extract the first_name into your firstname variable, the another extraction to extract the systolic_number into your systolic variable then extract the backslash using the char variable, and finally extract the diastolic_number into your diastolic variable.

Jim
Was This Post Helpful? 2
  • +
  • -

#9 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Re: Need help with file i/o program

Posted 17 April 2012 - 06:41 PM

View Postjimblumberg, on 17 April 2012 - 06:34 PM, said:

Why are you trying to ignore something. Don't you need all the information?

You have a file that contains the following infomation:
last_name first_name systolic_number/diastolic_number

So use the extraction operator>> to extract the last_name into your lastname variable, then use another extraction operation to extract the first_name into your firstname variable, the another extraction to extract the systolic_number into your systolic variable then extract the backslash using the char variable, and finally extract the diastolic_number into your diastolic variable.

Jim

yes i'm trying to extract all the data, but i want to only print the last name, first name then print the readings later. This is only a small portion of what i have to do. My final product should be a program which reads the data from a file and reads the systolic/diastolic data values and determines if they are normal or not normal and then print to the screen
"last name, first name has a normal/not normal blood pressure of systolic/diastolic"

What i was going to do was print the name first then do an if statement to read whether or not the sys/dia values are normal or not and then print the values.

Am i doing this wrong do you think or am i on the right track?
Was This Post Helpful? 0
  • +
  • -

#10 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 280
  • View blog
  • Posts: 1,787
  • Joined: 20-September 08

Re: Need help with file i/o program

Posted 17 April 2012 - 07:01 PM

This may help ...

#include <iostream>
#include <string>

using namespace std;


int main()
{
    while( true ) // loop forever ...
    {
        string s, dummy;
        int i = 0 , j = 0; // default values if cin >> intVar fails
        char c;

        // then print some instructions ...
        cout << "On one line: enter spaces, a word, space(s)\n"
             << "an int followed immediately by : "
             << "followed immediately by an int" << endl;
             
        // now get complex input using unique properties
        // of cin >> for int and for char and for string
        cin >> s >> i >> c >> j;
        
        if( !cin.good() ) cin.clear(); // clear any/all cin error flags
        
         // eat any char's in line after this including newline
        getline( cin, dummy ); // get ALL the rest of the line into string dummy
        cout << "You entered: " << s << ' ' << i << c << j << endl;
    }
}



Note that cin >> intVal

1. skips over any leading ws (whitespaces)
2. if it then meets a non-digit( not 0..9 ) it fails (and will fail until cin error flags cleared)
3. if it meets a digit, it will extract that digit and all sucessive digits into the intVal,
until a non-digit occurs (including any ws) NOTE:the remaining char's are left in the cin stream


Note that cin >> stringVal

1. skips over any leading ws (whitespaces)
2. if it then meets a non-ws it will extract all char's into stringVal until it meets a ws
NOTE:the remaining char's are left in the cin stream


Note that cin >> charVal

1. skips over any leading ws (whitespaces)
2. if it then meets a non-ws it will extract the first char into charVal
NOTE:the remaining char's are left in the cin stream
Was This Post Helpful? 1
  • +
  • -

#11 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Re: Need help with file i/o program

Posted 17 April 2012 - 07:09 PM

View Postjimblumberg, on 17 April 2012 - 06:34 PM, said:

Why are you trying to ignore something. Don't you need all the information?

You have a file that contains the following infomation:
last_name first_name systolic_number/diastolic_number

So use the extraction operator>> to extract the last_name into your lastname variable, then use another extraction operation to extract the first_name into your firstname variable, the another extraction to extract the systolic_number into your systolic variable then extract the backslash using the char variable, and finally extract the diastolic_number into your diastolic variable.

Jim


I forgot i took out the whitespace in my .dat file so when i used the string values it wasn't working, but now it is how i wanted to do it! Wow i feel dumb ha. Hopefully i can do some more of the programming for this program tonight and if i run into more problems i'll repost the new code tomorrow with what else i've got done and try to work through it. Thank you and everyone else very much

[/code]
Was This Post Helpful? 0
  • +
  • -

#12 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Re: Need help with file i/o program

Posted 17 April 2012 - 07:17 PM

sorry one last thing if anyone knows. How can i set up a while loop around the entire program to say if the EOF isn't reached keep reading the data and printing it to the screen. I tried while (file.!EOF)...but it comes up as an error and i don't know how to set that up.

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

/*
 * 
 */
int main() 
{
    //declare variables
    string lastname;
    string firstname;
    char trash;
    int sys;
    int dia;
    ifstream patient;
    
    
    
    //open file
    patient.open("patient.dat");
    
    patient>>firstname>>lastname>>sys>>trash>>dia;
    
    cout<<lastname;
    cout<<","<<firstname;
    
    
        if (sys < 120 && dia <80)
        {
            cout << " has a normal blood pressure of "<<sys<<trash<<dia;
        }
        else
        {
            cout<<" has a bad blood pressure of "<<sys<<trash<<dia;
        }
            
   
   

    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#13 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1312
  • View blog
  • Posts: 4,507
  • Joined: 19-February 09

Re: Need help with file i/o program

Posted 17 April 2012 - 08:20 PM

It is usually advised to use the read in the while expression like so :

while(patient>>firstname>>lastname>>sys>>trash>>dia)
{
  
}



The read returns true if the state of the stream is good. It includes checking the eof as well as bad read errors.
Was This Post Helpful? 1
  • +
  • -

#14 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 280
  • View blog
  • Posts: 1,787
  • Joined: 20-September 08

Re: Need help with file i/o program

Posted 17 April 2012 - 08:53 PM

But ... since that last item to input on each line from the stream is an int ...
You will need to deal with the char's left in that line in the stream after the first non-digit occurs, including any ws ...
So ...
while( fin >> firstnameStr >> lastnameStr >> sysInt >> trashChar >> diaInt )
{ 
   // process your data and output results ... 
   // but some where before next loop ...
   string dummyStr;
   getline( fin, dummyStr ); // will eat all char's and ws newline at end of each line
}



This is another good example of the computer programming reality of 'irreducible complexity'.

But once you have all these necessary parts in place, or equivalent alternate coding structures to fulfill the same task, ZOOM... it all works like a breeze :)

Oops: untested ... you may find that 'define's' simpler code works just fine ... since before it get's the next string, the first string on the next line it could/should skip over all leading whitespace until it hits the first non-ws.

This post has been edited by David W: 17 April 2012 - 09:24 PM

Was This Post Helpful? 1
  • +
  • -

#15 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1312
  • View blog
  • Posts: 4,507
  • Joined: 19-February 09

Re: Need help with file i/o program

Posted 17 April 2012 - 09:08 PM

firstname is a string will it not ignore the whitespace?

I suppose it depends on the data in the file.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2