![]() |
|
#1
|
|||
|
|||
Printing from a text fileBeing 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
|
|||
|
|||
Re: Printing from a text fileQuote:
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:
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
|
|||
|
|||
Re: Printing from a text filePlease see comments below for answers
Not sure how else to explain it Quote:
|
|
#4
|
|||
|
|||
Re: Printing from a text filePlease separate the comments rather than interspersing them in one large quote.
Quote:
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:
Here's the test.dat file: Code:
The output: Code:
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
|
|||
|
|||
Re: Printing from a text fileThanks Dave
Thats basically all i needed to know and sorry for not explaining it better Cheers |
Recent GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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