GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Oct-2007, 15:46
dragon71 dragon71 is offline
New Member
 
Join Date: Apr 2007
Posts: 7
dragon71 is on a distinguished road
Question

Printing from a text file


Being a noob i have a question i need to clear up

How do you open and print from a text file ??
Say i had a list Brisbane,Darwin,$1599
And i wanted to print it out under the headings
From Destination Cost

Brisbane Darwin $1599

Bit confused on printing lists in this format form that format


Thanks
  #2  
Old 07-Oct-2007, 17:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Printing from a text file


Quote:
Originally Posted by dragon71
...Say i had a list Brisbane,Darwin,$1599

Specify exactly how the items are going to appear in the file. You appear to have three items on a line, separated by commas. I'll call them fields.

So, each line of the data file consists of three fields, separated by commas. (At least that's what I see so far.)

Can you specify the input format more precisely and completely? Can there be spaces in the "from" and "destination fields", So, for example could the "from" field be "Upper Slobovia" and the "destination" field "Outer Elbonia". Or not? It could be important.

How are you going to store the data items in your program so that you can print them out?

Are you going to use std::string objects (This is the C++ forum, and that's the way that I think most C++ programmers would start, but sometimes when people are just beginning, their instructors tell them to use arrays of char.)

How big can the fields be? If you are going to use arrays of char, this is critical information. If you are going to use std::strings, it's no big deal for reading, but it could make a difference in the amount of work you have to do to figure out how to print them out neatly.

How about the "cost" field? Is it always going to be a Dollar Sign followed by an integer? Could it be a Dollar sign followed by an integer and a decimal point and two more decimal digits? What? Are you going to store the "cost" data item as a numerical quantity or just store it exactly as it appears in the input file? (What if someone left off the dollar sign? Is that allowed, or would that be considered a fatal error and your program would abort execution if that were not there.) That's the kind of things that you must think about before actually deciding how to write the program (let alone actually starting to write the program).


Quote:
Originally Posted by dragon71
And i wanted to print it out...

You have to define the output format before you can write a program to print it.

How wide are the output fields? If they are fixed and known before you read the file, that's one thing,

If you want to print them out neatly lined up and you don't know ahead of time what the field widths are, (and you want the output to accommodate the widest instance of each field), then you have to read all of the data items before you start printing.

That means that either you store them in some kind of array, or you read through the entire file to the end and keep track of the widest fields required, then "rewind" the file to the beginning and read them again so that you can print them out.

In summary:

I look at a program in its simplest terms as:

Input ==> Processing ==> Output

Define the input format and the output format. If they are fixed, then the "processing" step doesn't actually do anything. Otherwise there might be a little work.

Read up on <ifstream> objects so that you can open a file for reading.

Depending on how the input data are defined (and on data types you are going to use for your fields), you will use something like ">>" to read the items, or, possibly, one of the getline() functions. We can't possibly know what you are supposed to know or what you are allowed to use unless you tell us.

If you ask specific questions, I'll bet that there are lots of people here who would like to help.

Regards,

Dave
  #3  
Old 07-Oct-2007, 17:44
dragon71 dragon71 is offline
New Member
 
Join Date: Apr 2007
Posts: 7
dragon71 is on a distinguished road

Re: Printing from a text file


Please see comments below for answers
Not sure how else to explain it




Quote:
Originally Posted by davekw7x
Specify exactly how the items are going to appear in the file. You appear to have three items on a line, separated by commas. I'll call them fields.

So, each line of the data file consists of three fields, separated by commas. (At least that's what I see so far.) Yes

Can you specify the input format more precisely and completely? Can there be spaces in the "from" and "destination fields", So, for example could the "from" field be "Upper Slobovia" and the "destination" field "Outer Elbonia". Or not? It could be important. Yes, probably use setw ?

How are you going to store the data items in your program so that you can print them out? Brisbane, Darwin,$1599 laid out in the format so they line up under those headings

Are you going to use std::string objects (This is the C++ forum, and that's the way that I think most C++ programmers would start, but sometimes when people are just beginning, their instructors tell them to use arrays of char.) Yes,standard strings
How big can the fields be? If you are going to use arrays of char, this is critical information. If you are going to use std::strings, it's no big deal for reading, but it could make a difference in the amount of work you have to do to figure out how to print them out neatly.

How about the "cost" field? Is it always going to be a Dollar Sign followed by an integer? Could it be a Dollar sign followed by an integer and a decimal point and two more decimal digits? What? Are you going to store the "cost" data item as a numerical quantity or just store it exactly as it appears in the input file? (What if someone left off the dollar sign? Is that allowed, or would that be considered a fatal error and your program would abort execution if that were not there.) That's the kind of things that you must think about before actually deciding how to write the program (let alone actually starting to write the program). Prefer it to be double or float




You have to define the output format before you can write a program to print it.

How wide are the output fields? If they are fixed and known before you read the file, that's one thing,

If you want to print them out neatly lined up and you don't know ahead of time what the field widths are, (and you want the output to accommodate the widest instance of each field), then you have to read all of the data items before you start printing.

That means that either you store them in some kind of array, or you read through the entire file to the end and keep track of the widest fields required, then "rewind" the file to the beginning and read them again so that you can print them out.

In summary:

I look at a program in its simplest terms as:

Input ==> Processing ==> Output

Define the input format and the output format. If they are fixed, then the "processing" step doesn't actually do anything. Otherwise there might be a little work.

Read up on <ifstream> objects so that you can open a file for reading.
Need to know how to print to screen under the headings From:Brisbane etcHow do i separate abarisbane fron Darwin from $1599 when i read it in to put it under those headings ???
Depending on how the input data are defined (and on data types you are going to use for your fields), you will use something like ">>" to read the items, or, possibly, one of the getline() functions. We can't possibly know what you are supposed to know or what you are allowed to use unless you tell us.

If you ask specific questions, I'll bet that there are lots of people here who would like to help.

Regards,

Dave
  #4  
Old 07-Oct-2007, 19:07
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Printing from a text file


Please separate the comments rather than interspersing them in one large quote.
Quote:
Originally Posted by dragon71
Need to know how to print to screen under the headings From:Brisbane etcHow do i separate abarisbane fron Darwin from $1599 when i read it in to put it under those headings ???

As I tried to explain. There are three parts of the program.

1. Read input data (from file in this case)
2. Do whatever you need to do with it (nothing, I guess)
3. Write output data (to console, I guess)

1. To read input data, open a file, use getline to read the individual fields into separate variables, a line at a time.

2. SInce you don't do anything other than print out the lines, and you didn't say anything about what the maximum field widths are, even though I asked, then I will just assume that there is nothing to do.

3. Print out the values of the fields, spaces however you need them to line up under the column headers.

(Where I will assume that you print out the column headers before you get into a loop to read the lines from the file.

Here is one way to get started:
CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    string origin, destination, cost;
    char *inname = "test.dat";

    ifstream infile(inname);
    if (!infile) {
        cout << "There was a problem opening file " << inname << " for reading."
             << endl;
        return 0;
    }
    cout << "Opened " << inname << " for reading" << endl;

    int recordcount = 0;
    cout << "Origin            Destination              Cost" << endl;
    cout << "-----------------------------------------------" << endl;

    while (infile) {
        getline(infile, origin,',');
        if (!infile) {
            break;
        }
        ++recordcount;
        cout << "Record number " << recordcount << endl;
        cout << "  origin      = <" << origin << ">" << endl;
        getline(infile, destination, ',');
        if (!infile) {
            cout << "Problem reading destination." << endl;
            break;
        }
        cout << "  destination = <" << destination << ">" << endl;
        getline(infile, cost);
        if (!infile) {
            cout << "Problem reading cost " << endl;
            break;
        }
        cout << "  cost        = <" << cost << ">" << endl;
        // later will put print statement for output
        cout << endl;
       
    }
    cout << "Number of records = " << recordcount << endl;
    return 0;
}

Here's the test.dat file:
Code:
Brisbane, Darwin, $1500 Upper Slobovia,Outer Elbonia,$325.19

The output:

Code:
Opened test.dat for reading Origin Destination Cost ----------------------------------------------- Record number 1 origin = <Brisbane> destination = < Darwin> cost = < $1500> Record number 2 origin = <Upper Slobovia> destination = <Outer Elbonia> cost = <$325.19> Number of records = 2

My printout shows the fields of each record, as I suggested in my previous post. You must decide whether I have used appropriate settings for the column headings and then use whatever you have to use to get the output fields lines up. Look carefully at the input file records and fields. What do you need to do to get consistent results? I hate to repeat myself, but you must be very specific as to what the input records will look like.

Regards,

Dave
  #5  
Old 07-Oct-2007, 20:55
dragon71 dragon71 is offline
New Member
 
Join Date: Apr 2007
Posts: 7
dragon71 is on a distinguished road

Re: Printing from a text file


Thanks Dave
Thats basically all i needed to know and sorry for not explaining it better

Cheers
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Help! Problems encountered while burning CD's RayDarkness Computer Software Forum - Windows 1 18-Nov-2006 04:54
How to read unknown total of int data from text file to a 2-dim array in C or C++? ladyscarlet99 C Programming Language 2 09-Sep-2005 14:07
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:13.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.