![]() |
|
#1
|
|||
|
|||
Need Help in displaying file contents.The file that I have has the following format of the customers :-
Customer file format: <id>,<surname>,<firstname>,<address>,<suburb>,<pos tcode>, <phone> Example: C0001,Bloggs,Joe,123 Long Street,Melbourne,3000,93005555 I could display them in the same format but I want to display them as follewing:- Example: ID Surname FirstName Address Suburb Post Phone ----- ------------ -------- -------------------- C0001 Bloggs Joe 123 Long Street Melbourne 3000 93005555 ... ... ... ... ... ... ... I have written some of the structures for this: CPP / C++ / C Code:
I would really appreciate anyone who helps me with this one. Thanks |
|
#2
|
|||
|
|||
Re: Need Help in displaying file contents.Help with what? You need to post the code you are having trouble with, or at least need help with. There's nothing in your post for us to work with, code-wise. See Guideline #2
__________________
Please read http://www.gidforums.com/t-5566.html. They were written to help you create a request that is readable and has enough information we can actually tell what you need help with. |
|
#3
|
|||
|
|||
Re: Need Help in displaying file contents.Please mention what language you wish to use. What you have is both valid C and valid C++.
Have you tried anything other than the structure of the data (code that is)? Try to implement a small main funtion that reads the lines of the file and simple prints them back out to the screen. Once this is working, there are many ways to parse strings. Get that working and we can help you from there. |
|
#4
|
|||
|
|||
Re: Need Help in displaying file contents.I have used the following code in order to display the file as it is........but can't display it in the formatted manner.
The code is obviously in C. The ts.h is just the file I have specified above as structures. CPP / C++ / C Code:
Thanks. |
|
#5
|
||||
|
||||
Re: Need Help in displaying file contents.hi,
use fread and fwrite to read and write from a file... the syntax of fread and fwrite is CPP / C++ / C Code:
buffer Pointer to data to be written. size Item size in bytes. count Maximum number of items to be written. stream Pointer to FILE structure for example, if u want to read 5 items of type customernode then the code would be CPP / C++ / C Code:
|
|
#6
|
||||
|
||||
Re: Need Help in displaying file contents.Quote:
What 'read/input' commands have you been taught? You need to read a line of the file (probably using fgets(), it's safest and easiest. Then you have to split up the line read into your customerNode fields (each with a \0 (NULL) at the end). Then you can print out each field at will (or on the monitor if you prefer -- it's hard to read Will). __________________
Age is unimportant -- except in cheese |
|
#7
|
||||
|
||||
Re: Need Help in displaying file contents.Below is an example of file reading using fgets.
You have to check that your file was opened succesfully before working with the file, or your application will be terminated faster than a Jedi knight terminates an imperial robot MAX_LENGTH is just a constant I defined for my application. modify it. Also, point to the name of your file. CPP / C++ / C Code:
Best regards, Kobi Hikri. __________________
It's actually a one time thing (it just happens alot). |
|
#8
|
|||
|
|||
Re: Need Help in displaying file contents.Quote:
Here is his typical line from the input file. Since the actual sizes of customer name fields, address field, etc. are not known, I can't see any practical reason to use fread to get the information. Code:
Quote:
Sorry, Not Even Close, for Several Big Reasons: CPP / C++ / C Code:
My output: Quote:
Regards, Dave |
|
#9
|
||||
|
||||
Re: Need Help in displaying file contents.Quote:
Hey Dave. Could you please explain this point ? I usually use fgets and strtok to read and decompose lines from a file, but fgets is also being bounded by an upper bound ... Is there away to read lines without an upper bound ? I mean, I do need to declare a fixed size buffer before reading into it (or not ?). Kobi Hikri. __________________
It's actually a one time thing (it just happens alot). |
|
#10
|
||||||
|
||||||
Re: Need Help in displaying file contents.Quote:
The answer to that question is: "it depends". In order to use fread(), you have to know exactly how many bytes to read. This is sometimes useful if your input fields always have an exact, known length. Take the surname field: its buffer size is 13 (max length of the name is 12 chars + one char required for the terminating '\0'). The input line given in the example has a surname that was six chars long. If you use fread() to read the number of bytes in the given struct, they won't end up in the proper fields. Unless, that is, you make sure that the files were written with all fields padded out to the correct length (with leading zeros for numeric fields and trailing blanks for character fields, for example.) This was not the case for the example input line given by the Original Poster. Therefore my comment that I don't think it's very practical to use fread() for this kind of data base. Possible? yes. Practical? no. (Well, that's my opinion anyhow.) For C++ programs, getline with a std::string allows inputs up to the limits of memory in your system (Actually, probably limited to a few gigabytes in 32-bit systems.) For C programs, you could use fgets() into a buffer of size BUFSIZ (defined in <stdio.h> The value of BUFSIZ is implementation-dependent, and there is even a function that you can use to make it whatever value you want. I'll leave it up to you to find it --- I personally have never found a reason to change the size of stream I/O buffers but ... (You can investigate properties and implications of BUFSIZ if you are really interested.) If the user input line is greater than or equal to BUFSIZ, then the input line will not have a '\n' char (it is always terminated by a \'0') so you can detect and and you may be able to handle such extremenly long cases, if you need to. I have found that some compilers have functions that simply stop accepting characters if more than BUFSIZ-1 are entered on a given input line, and others apparently have an even larger buffer somewhere that keeps on storing additional chars, so if your program does encounter a case of extremely long input lines, recovery of lost input may or may not be possible. CPP / C++ / C Code:
I ran this program (Borland compiler) and camped on the 'a' key until it stopped echoing 'a' to the screen. Quote:
I got several rows of 'a's here, then it stopped echoing. I released the 'a' key and pressed 'Enter'. Here's what it showed me: Quote:
Quote:
Now, with the same program, compiled with Microsoft Visual C++ here's the result: Quote:
Here I held the 'a' key down for about seven complete rows (80 columns per row) on my console window. It never stopped echoing chars, so I released the key and pressed 'Enter'. Then all of the following spilled out (didn't wait for additional characters) ... Quote:
gcc did the same, except BUFSIZ was shown to be 1024 instead of 512. So, if you have really long lines of input and you want to use fgets and you want to make sure that you don't drop any chars, you have a problem. Then, maybe you have to use fread to read smaller chunks and process the input accordingly. (Using combinations of sscanf, strtok, etc.) Or, as in the case of encryption/decryption, read one char at a time (using getchar()) into an internal buffer and process the chars or blocks of chars as they appear. In cases like the example of the Original Post, any really long input lines (longer than the size of the struct) are an indication of a bad data base, and programs like this typically just report an error (giving the line number), and expect the user to resort to some other program or method (manual inspection of the file) to recover. Regards, Dave |
Recent GIDBlog
Welcome to Baghdad 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 |
| Made program in Java, trying C++ now, file i/o problems | technickel | C++ Forum | 4 | 19-Feb-2005 00:32 |
| 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