• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

easy one -file I/O - 300 points

Hi, I have a file of this structure:

1$data
2$aadad
;this is a comment line
3$azdfadad
;here is another one
5$6
6$azd

and I want to do the following:
(a)
read in the file, and display all the lines which aren't comment:
1...data
2...aadad
3...azdfadad
5...6
6...azd
where the ... is a \t (it isn't possible to post a sequence of spaces here)
(b)
every line which isn't comment should be red in as an integer and a string (and, the integer can be >9 , so I think I should work with something like " fscanf (Fp, "%d$%s",&dd, aaa); ". They should be (temporarily) saved in variables (int tempint; and char tempchar[100]; for example)
(c)
I prefer NOT to read in the integer as a character and convert it with atoi (because I hate using atoi).

this question is allso asked here:
http://oldlook.experts-exchange.com:8080/Programming/Programming_Languages/C/Q_21406661.html
HOWEVER: I misformulated the question at first, so please read the whole thread if you can replie there (because , I have about 10 versions which do the wrong thing now (my fault, I didn't asked it correct ) )

Thanks in advance.
0
André123
Asked:
André123
3 Solutions
 
grg99Commented:
char TheLine[1000];
int p;

    fscanf( Fp, "%s\n", TheLine );

if( TheLine[0] == ';' ) {  // ignore it
 }
else
{
    p = 0;
    while( isdigit( theLine[p] ) p++;
   TheLine[p] = '\t';
    printf( "%s\n", TheLine );
}
0
 
sunnycoderCommented:
>I prefer NOT to read in the integer as a character and convert it with atoi (because I hate using atoi).
Unfortunately, that is the best way out since there is no way to know in advance if firct character on a line would be an int or ; ... Here are a couple of versions

FILE * buffer;
char buffer[100];
int tempint;
char * temp;

fptr = fopen ("myfile", "r");
...

while (fgets(buffer, 100, fptr)!= NULL)
{
        if (buffer[0] == ';' )
             continue;

        tempint = strtol (buffer, 0, NULL );
 
        temp = strchr (buffer, '$' );
        temp ++;
        printf ("%d\t%s\n",tempint,temp);
}
...
fclose (fptr);
....


Version 2:

FILE * buffer;
char buffer[100];
int tempint;
char * temp;

fptr = fopen ("myfile", "r");
...

while (fgets(buffer, 100, fptr)!= NULL)
{
        if (buffer[0] == ';' )
             continue;
 
        temp = strchr (buffer, '$' );
         * temp = '\t';
        printf ("%s\n",temp);
}
...
fclose (fptr);
0
 
smpoojaryCommented:
I think following code is sufficent for you.

#include <stdio.h>
int main()
{
      char caTheLine[100],caContent[100];
      int nId;
      FILE *Fp;

      if((Fp = fopen("in.txt","r")) == NULL)
      {
            printf("Error unable to open input file");
            return 1;
      }

      while(!feof(Fp))
      {
            if(fgets(caTheLine,100,Fp) == NULL) break;
            if( caTheLine[0] == ';' ) continue;
            sscanf(caTheLine,"%d$%s",&nId,caContent);
            printf("%d\t%s\n",nId,caContent);
      }
      return 0;
}
-Mahesh
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
smpoojaryCommented:
Any doubt
0
 
André123Author Commented:
I used the code of smpoojary, this one does exactly what I needed
sunnycoder -> didn't gave the int's as int
grg99->allso needed further processing to convert the numbers to an int ...

Thanks all.
0
 
André123Author Commented:
PS: smpoojary, if you drop a line here http://oldlook.experts-exchange.com:8080/Programming/Programming_Languages/C/Q_21406661.html I give you some more points.
0
 
smpoojaryCommented:
Thanks a lot
-Mahesh
0
 
smpoojaryCommented:
I have put comment in that link.
-Mahesh
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

KuppingerCole Reviews AlgoSec in Executive Report

Leading analyst firm, KuppingerCole reviews AlgoSec's Security Policy Management Solution, and the security challenges faced by companies today in their Executive View report.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now