?
Solved

Best way to achieve reading into structure..

Posted on 2000-03-20
4
Medium Priority
?
241 Views
Last Modified: 2010-04-15
hi guys,

I have a predetermined format in a text file which I will use to update a structure.
The format is:
--------------------------------
H,%s,%s,%s,%s,%s
D,%s,%s,%s
T,%s

H,%s,%s,%s,
%s,%s
D,%s,%s,%s
T,%s
... and so on..

H denotes header, D details and T trailer.. the number of fields are fixed. What is the best way to read in into a structure???
1) fscanf (myfile, "%s,%s,...
2) read char by char and look out for the commas, then "cut" out the field values and insert into the record field.

NOte: STRTOK is not used because it cannot differentiate ",," where two delimiters are together...

Please advice from the design point of view, and also a skeleton of your algorithm to do it the best way...

Thanks!


0
Comment
Question by:Haho
4 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 2638720
Given the way you've described the format, scanf certainly comes to mind as a way to read them.
Is there some problem with using scanf?
0
 
LVL 3

Expert Comment

by:terajiv
ID: 2638930
Hi Haho,

Is it compulsory to go for C? if u go for C++ u can  read the file in blocks or as objects which will help u alot and will minimise ur headache.

But ur sticking to C then :
Pl. clarify some doubts...
in a text file r u putting

>>H,%s,%s,%s,%s,%s
>>D,%s,%s,%s
>>T,%s

this for all the Records if yes then what is the functionality of that?
If u want it tobe there and according to the letters H, D, T u will put data in disserent Structures then u can go for ur second option.
Read the file when H encounters start putting the chars in array till u get comma. like this get all cntents of one structure store it then proceed further...
I hope it is clear to u...

All the Best
Have A NIce Time

Rajiv
0
 
LVL 2

Accepted Solution

by:
Andrei Rodionov earned 400 total points
ID: 2639291
Hi Haho!

First of all you can *not* use fscanf function to read your fields like 'H,%s,%s,%s,%s,%s'. So you have no choice but to handle every char. For example,

File skel.dat
_____________

H,One,Two,,Four,Five
D,Banana,,Apple
T,Hello

H,Oracle,Sybase,Informix,Progress,Access
D,Fast,Scalable,Reliable
T,Goodbye

File skel.c
_____________

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void load_header( void );

#define EOS                     '\0'

#define MAX_LENGTH              32

typedef struct _test_header {
  char szOne[MAX_LENGTH];
  char szTwo[MAX_LENGTH];
  char szThree[MAX_LENGTH];
  char szFour[MAX_LENGTH];
  char szFive[MAX_LENGTH];
} test_header;

typedef struct _test_detail {
  char szOne[MAX_LENGTH];
  char szTwo[MAX_LENGTH];
  char szThree[MAX_LENGTH];
} test_detail;

typedef struct _test_trailer {
  char szOne[MAX_LENGTH];
} test_trailer;

char szBuffer[512];
char cAbbrev, cSeparator;

FILE* pfTest;
test_header* pHeader;
test_detail* pDetail;
test_trailer* pTrailer;

int main()
{
  pfTest = fopen( "skel.dat", "rt" );
  if ( pfTest != NULL ) {
    pHeader = (test_header*)calloc( 1, sizeof(test_header) );
    pDetail = (test_detail*)calloc( 1, sizeof(test_detail) );
    pTrailer = (test_trailer*)calloc( 1, sizeof(test_trailer) );
    if ( pHeader != NULL || pDetail != NULL || pTrailer != NULL ) {
      do {
        fgets( szBuffer, sizeof(szBuffer), pfTest );
        if ( szBuffer[0] == 'H' && szBuffer[1] == ',' ) {
          load_header();
          printf( "Header: %s, %s, %s, %s, %s\n", pHeader->szOne,
                                                  pHeader->szTwo,
                                                  pHeader->szThree,
                                                  pHeader->szFour,
                                                  pHeader->szFive );
          memset( pHeader->szOne, EOS, sizeof(pHeader->szOne) );
          memset( pHeader->szTwo, EOS, sizeof(pHeader->szTwo) );
          memset( pHeader->szThree, EOS, sizeof(pHeader->szThree) );
          memset( pHeader->szFour, EOS, sizeof(pHeader->szFour) );
          memset( pHeader->szFive, EOS, sizeof(pHeader->szFive) );
        }
      } while ( !feof( pfTest ) );
      free( pTrailer );
      free( pDetail );
      free( pHeader );
    }
    fclose( pfTest );
  }
  return 0;
}

void load_header( void )
{
  char* pszCurrent;
  char* pszStart;
  char* pszEnd;
  int iFldCount = 0;

  pszStart = szBuffer + 2;
  pszEnd = pszStart;
  pszCurrent = pszStart;
  while ( iFldCount <= 5 ) {
    if ( *pszCurrent == ',' || *pszCurrent == EOS ) {
      pszEnd = pszCurrent;
      ++iFldCount;
      switch ( iFldCount ) {
      case 1:
        strncpy( pHeader->szOne, pszStart, pszEnd - pszStart );
        break;
      case 2:
        strncpy( pHeader->szTwo, pszStart, pszEnd - pszStart );
        break;
      case 3:
        strncpy( pHeader->szThree, pszStart, pszEnd - pszStart );
        break;
      case 4:
        strncpy( pHeader->szFour, pszStart, pszEnd - pszStart );
        break;
      case 5:
        strncpy( pHeader->szFive, pszStart, pszEnd - pszStart );
        if ( pHeader->szFive[pszEnd-pszStart-1] == '\n' )
          pHeader->szFive[pszEnd-pszStart-1] = EOS;
        break;
      }
      pszStart = pszCurrent + 1;
    }
    ++pszCurrent;
  };
  memset( szBuffer, EOS, sizeof(szBuffer) );
  return;
}

That program output is

Header: One, Two, , Four, Five
Header: Oracle, Sybase, Informix, Progress, Access

Ready to answer,
Andrew
0
 
LVL 1

Author Comment

by:Haho
ID: 2663885
thanks...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question