Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

storing data to a buffer

Status
Not open for further replies.

satellite03

IS-IT--Management
Dec 26, 2003
248
IN

i have data file x.dat A 1 2 3 4 B 2 5 C .....

i want to store those in the buffer and also i want to print those....how can i do it??

i find it difficult bcoz data are not homogeneous(e.g char and int mixed ) ..so how can i do this???

 
if the data in the file is actually in text format as you have posted, you can simply use fscanf and print the 'data' as a string.
 
....but i need to store in a buffer...

you are saying to read in this format >>> fscanf(infile,"%c",&var); ...ok.. as far as printing is concerned there won't be any problem...but i want to processes the data.
say i have a two digit(e.g 35) number like below...

A 1 2 35 4 C D 2 6 ....

if i read that way then i will read 3 and 5 as character(not 35 as a whole as integer) so i will have to face problem in processing the data...

let me tell you more details...say i have

A 1 2 4 12 14 15 18 a b C 14 14 15..

i want to see how many times A(this is a char) has been repeated, how many times integer 14 ( not 1 & 4) has been repeated...

thats why i told i need to store data...
may be its not a good way of thinking.... so i am putting my problem in a simple way



i have
my data
-------
A 1 2 4 12 14 15 18 a b C 14 14 15..

what i want
-------
how can i calculate how many times individuals are repeating???

can you suggest how can i do this??
thanks



 
hi, can anybody help me please...

which way i should read ? fscanf(infile,"%c",&var) or fscanf(infile,"%d",&var)...

in any case i have to scan the elements .....which format i should use???
 
You'll find it difficult to do what you want using just fscanf(), since it's pretty much a one-shot deal.

Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int count_letters ( char *line, char letter ) {
  int count = 0;
  while ( *line != '\0' ) {
    if ( *line == letter ) count++;
    line++;
  }
  return count;
}
int count_numbers ( char *line, int num ) {
  int count = 0;
  while ( *line != '\0' ) {
    int value;
    while ( !isdigit(*line) && *line != '\0' ) line++;  // find a number
    value = strtoul( line, &line, 10 );
    if ( value == num ) count++;
  }
  return count;
}

int main(void) {
  // use a loop, and use fgets() to read one line from your file
  char buff[] = &quot;A 1 2 4  12  14 15 18  a b C 14 14 15\n&quot;;
  int letters = count_letters ( buff, 'A' );
  int numbers = count_numbers ( buff, 14 );
  printf( &quot;line %s contains %d letters\n&quot;, buff, letters );
  printf( &quot;line %s contains %d numbers\n&quot;, buff, numbers );
  return 0;
}

By using a loop like
Code:
char buff[BUFSIZ];
while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
  int letters = count_letters ( buff, 'A' );
  int numbers = count_numbers ( buff, 14 );
}

You can have as many goes at decoding the buffer as you like.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top