GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Sep-2005, 21:10
bhullar_v bhullar_v is offline
New Member
 
Join Date: Sep 2005
Posts: 2
bhullar_v is on a distinguished road

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:
/* Constants for customer information. */
#define CUSTID_LEN 5
#define SURNAME_MAX 12
#define FIRSTNAME_MAX 12
#define ADDRESS_MAX 20
#define SUBURB_MAX 12
#define POSTCODE_LEN 4
#define PHONENUM_LEN 8

typedef struct customerNode* CustomerNodePtr;
typedef struct stockNode* StockNodePtr;

/* Structure definitions. */
typedef struct customerNode
{
   char custID[CUSTID_LEN + 1];
   char surname[SURNAME_MAX + 1];
   char firstName[FIRSTNAME_MAX + 1];
   char address[ADDRESS_MAX + 1];
   char suburb[SUBURB_MAX + 1];
   unsigned postCode;
   unsigned phoneNum;
   CustomerNodePtr nextCust;
} CustomerNodeType;

I would really appreciate anyone who helps me with this one.
Thanks
  #2  
Old 24-Sep-2005, 21:18
Guidelines Plz Guidelines Plz is offline
Junior Member
 
Join Date: Sep 2005
Posts: 87
Guidelines Plz is on a distinguished road

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  
Old 24-Sep-2005, 21:32
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 116
L7Sqr has a spectacular aura about

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  
Old 24-Sep-2005, 23:05
bhullar_v bhullar_v is offline
New Member
 
Join Date: Sep 2005
Posts: 2
bhullar_v is on a distinguished road

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:
#include<stdio.h>
#include"ts.h"

int main(void)

{

FILE *fp;

char c;
fp = fopen("customer.csv", "r");
do
putchar(c = getc(fp));
while(c != EOF);
return EXIT_SUCCESS;
}

Thanks.
  #5  
Old 24-Sep-2005, 23:26
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

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:
size_t fread( 
   void *buffer,
   size_t size,
   size_t count,
   FILE *stream 
);

size_t fwrite(
   const void *buffer,
   size_t size,
   size_t count,
   FILE *stream 
);

where

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:
FILE *fp;
customernodeptr cptr;
fp = fopen("customer.csv", "r");
fread(cptr,sizeof(cptr),5,fp);
then u can print as required.
  #6  
Old 25-Sep-2005, 01:39
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,242
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Need Help in displaying file contents.


Quote:
Originally Posted by bhullar_v
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:
#include<stdio.h>
#include"ts.h"

int main(void)

{

FILE *fp;

char c;
fp = fopen("customer.csv", "r");
do
putchar(c = getc(fp));
while(c != EOF);
return EXIT_SUCCESS;
}

Thanks.

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  
Old 25-Sep-2005, 06:32
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

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:
FILE *ifp;
ifp = fopen(argv[argument_index],"r");
if (ifp)
   {
      char current_line[MAX_LENGTH];
      //File opened successfully.
      while (fgets(current_line,MAX_LENGTH,ifp))
      {
      //Do whatever you want to do with your record
      }
   }

Best regards,
Kobi Hikri.
__________________
It's actually a one time thing (it just happens alot).
  #8  
Old 25-Sep-2005, 09:04
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: Need Help in displaying file contents.


Quote:
Originally Posted by Paramesh
use fread and fwrite to read and write from a file...

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:
C0001,Bloggs,Joe,123 Long Street,Melbourne,3000,93005555

Quote:
Originally Posted by Paramesh

for example, if u want to read 5 items of type customernode then the code would be
CPP / C++ / C Code:
FILE *fp;
customernodeptr cptr;
fp = fopen("customer.csv", "r");
fread(cptr,sizeof(cptr),5,fp);

Sorry, Not Even Close, for Several Big Reasons:

CPP / C++ / C Code:
#include <stdio.h>
/* Constants for customer information. */
#define CUSTID_LEN 5
#define SURNAME_MAX 12
#define FIRSTNAME_MAX 12
#define ADDRESS_MAX 20
#define SUBURB_MAX 12
#define POSTCODE_LEN 4
#define PHONENUM_LEN 8

typedef struct customerNode* CustomerNodePtr;
typedef struct stockNode* StockNodePtr;

/* Structure definitions. */
typedef struct customerNode
{
   char custID[CUSTID_LEN + 1];
   char surname[SURNAME_MAX + 1];
   char firstName[FIRSTNAME_MAX + 1];
   char address[ADDRESS_MAX + 1];
   char suburb[SUBURB_MAX + 1];
   unsigned postCode;
   unsigned phoneNum;
   CustomerNodePtr nextCust;
} CustomerNodeType;

int main()
{
  FILE *fp;
  CustomerNodePtr cptr;
  printf("sizeof(cptr) = %d\n", sizeof(cptr));

  //fp = fopen("customer.csv", "r");
  //fread(cptr,sizeof(cptr),5,fp); <<== don't do this: it's disaster waiting to happen!!!!!!!!

  printf("Number of bytes that would have been read = %d\n", 5 * sizeof(cptr));
  printf("1. Does that seem right?\n");
  printf("2. Where would they have been put???\n");
  return 0;
}

My output:

Quote:
sizeof(cptr) = 4
Number of bytes that would have been read = 20
1. Does that seem right?
2. Where would they have been put???

Regards,

Dave
  #9  
Old 25-Sep-2005, 09:14
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Need Help in displaying file contents.


Quote:
Originally Posted by davekw7x
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.

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  
Old 25-Sep-2005, 10:22
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: Need Help in displaying file contents.


Quote:
Originally Posted by kobi_hikri
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.

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:
#include <stdio.h>
#include <string.h>
int main()
{
  char inbuf[BUFSIZ];
  printf("BUFSIZ = %d\n", BUFSIZ);
  printf("Hold some key down until at least %d chars have been echoed to the screen:\n", BUFSIZ);
  fgets(inbuf, BUFSIZ, stdin);
  printf("1: strlen(inbuf) = %d\n", strlen(inbuf));
  printf("If you held the key for more than %d chars, what happened to the rest?\n", BUFSIZ-1);
  printf("If your system is now waiting for input, the extra chars are gone, gone, gone!\n");
  fgets(inbuf, BUFSIZ, stdin);
  printf("2: strlen(inbuf) = %d\n", strlen(inbuf));

  return 0;
}


I ran this program (Borland compiler) and camped on the 'a' key until it stopped echoing 'a' to the screen.

Quote:
BUFSIZ = 512
Hold some key down until at least 512 chars have been echoed to the screen:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
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:
1: strlen(inbuf) = 511
If you held the key for more than 511 chars, what happened to the rest?
If your system is now waiting for input, the extra chars are gone, gone, gone!
The system was waiting for more input, so I typed Bye, and here's what it said:
Quote:
2: strlen(inbuf) = 4

Now, with the same program, compiled with Microsoft Visual C++ here's the result:
Quote:
BUFSIZ = 512
Hold some key down until at least 512 chars have been echoed to the screen:
...
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:
1: strlen(inbuf) = 511
If you held the key for more than 511 chars, what happened to the rest?
If your system is now waiting for input, the extra chars are gone, gone, gone!
2: strlen(inbuf) = 50

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 GIDBlogWelcome to Baghdad 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
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

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


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