Solved

Best way to achieve reading into structure..

Posted on 2000-03-20
4
215 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 84

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:
AndrewRodionov earned 100 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
memory mapped I/O query 6 147
How to get time_t value that is a week old 6 168
chcp 65001 File encoding 66 247
C language IDE – Compilers installation 14 71
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

809 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