Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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