André123
asked on
fscanf - easy
Hi, I have a datafile of this form:
1$zfzdf
2$aadad
0$ert
3$azdfadad
0$4$44
5$6
6$azd
where every line which starts with 0$ is comment.
I want it to be of this form:
1$zfzdf
2$aadad
;ert
3$azdfadad
;4$44
5$6
6$azd
How should I adapt this code:
int dd;
char aaa[100];
while (!feof(Fp)) {
fscanf (Fp, "%d$%s",&dd, aaa);
if(dd!=0){printf("%d\t%s\n ",dd,aaa); }
Sleep(200);
}
I tried to make something with fgetpos and fsetpos, but didn't succeeded....
Thanks in advance.
1$zfzdf
2$aadad
0$ert
3$azdfadad
0$4$44
5$6
6$azd
where every line which starts with 0$ is comment.
I want it to be of this form:
1$zfzdf
2$aadad
;ert
3$azdfadad
;4$44
5$6
6$azd
How should I adapt this code:
int dd;
char aaa[100];
while (!feof(Fp)) {
fscanf (Fp, "%d$%s",&dd, aaa);
if(dd!=0){printf("%d\t%s\n
Sleep(200);
}
I tried to make something with fgetpos and fsetpos, but didn't succeeded....
Thanks in advance.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
a good way should be...
while(!feof(fp)) {
fgets(str,255,fp);
if (!(str[0]=='0' && str[1]=='$'))
printf("%s",str);
}
hope that helps.
regards,
cryptosid
while(!feof(fp)) {
fgets(str,255,fp);
if (!(str[0]=='0' && str[1]=='$'))
printf("%s",str);
}
hope that helps.
regards,
cryptosid
ASKER
thanks, cryptosid, but the problem is that I need to have the number which is in the line (just before the $-sign).
I know it's possible with atoi to convert the first part of the string to a number, but I don't really like to use atoi ....
So, to make thing clear,
1$zfzdf
2$aadad
;ert
3$azdfadad
;4$44
5$6
6$azd
should display:
1...zfzdf
2...aadad
3...azdfadad
5...6
6...azd
where the ... is allways a tab (tabs dissapear when I post them here on the forum.
Thanks a lot.
I know it's possible with atoi to convert the first part of the string to a number, but I don't really like to use atoi ....
So, to make thing clear,
1$zfzdf
2$aadad
;ert
3$azdfadad
;4$44
5$6
6$azd
should display:
1...zfzdf
2...aadad
3...azdfadad
5...6
6...azd
where the ... is allways a tab (tabs dissapear when I post them here on the forum.
Thanks a lot.
adapted from crypto:
fgets(str,255,fp);
if (!(str[0]=='0' && str[1]=='$'))
printf("%c\t%s",str[0], str+2);
}
jaydutt
fgets(str,255,fp);
if (!(str[0]=='0' && str[1]=='$'))
printf("%c\t%s",str[0], str+2);
}
jaydutt
ASKER
Sorry I'm that anoying, but I want to display those numbers indeed (that part is OK); but there are two more "problems" :
1/ the numbers can go till 1000 (-> it's not allways the first char only); that was the reason why I thought I should use fscanf (because then you get this automaticly)
2/ I need the numbers to be available as number (int) allso (and, if possible, I prefer not to use atoi [ if there is no other solution, I guess I'll have to use it...] )
Thanks for all your help all; and sorry I didn't described the problem good enough in my question .
1/ the numbers can go till 1000 (-> it's not allways the first char only); that was the reason why I thought I should use fscanf (because then you get this automaticly)
2/ I need the numbers to be available as number (int) allso (and, if possible, I prefer not to use atoi [ if there is no other solution, I guess I'll have to use it...] )
Thanks for all your help all; and sorry I didn't described the problem good enough in my question .
There IS a return code to fscanf. It is the number of formats satisfied (or, on some systems, the number of variables filles. You code may be better like this:
int dd;
char aaa[100];
while ( fscanf (Fp, " %d$%s\n",&dd, aaa) >= 1 )
{
if(dd!=0){printf("%d\t%s\n ",dd,aaa); }
}
Sleep(200);
int dd;
char aaa[100];
while ( fscanf (Fp, " %d$%s\n",&dd, aaa) >= 1 )
{
if(dd!=0){printf("%d\t%s\n
}
Sleep(200);
much better, paul.
ASKER
thanks a lot PaulCaswell, I'll try it monday (I don't have a compiler installed on my computer at home).
but are you sure that :
1$zfzdf
2$aadad
;ert
3$azdfadad
;4$44
5$6
6$azd
displays:
1...zfzdf
2...aadad
3...azdfadad
5...6
6...azd
because it seems to me that it only displays
1...zfzdf
2...aadad
3...azdfadad
5...6
6...azd
for
1$zfzdf
2$aadad
0$ert
3$azdfadad
0$4$44
5$6
6$azd
and, as you could read some posts above, this wasn't what I needed...
but are you sure that :
1$zfzdf
2$aadad
;ert
3$azdfadad
;4$44
5$6
6$azd
displays:
1...zfzdf
2...aadad
3...azdfadad
5...6
6...azd
because it seems to me that it only displays
1...zfzdf
2...aadad
3...azdfadad
5...6
6...azd
for
1$zfzdf
2$aadad
0$ert
3$azdfadad
0$4$44
5$6
6$azd
and, as you could read some posts above, this wasn't what I needed...
>>but are you sure that ...
No! The return value of fscanf can be a bit odd on some systems. It MIGHT work! If it does it will be a neat solution. Try it.
Paul
No! The return value of fscanf can be a bit odd on some systems. It MIGHT work! If it does it will be a neat solution. Try it.
Paul
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 );
}
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 );
}
I think following code is sufficent for you.
#include <stdio.h>
int main()
{
char caTheLine[100],caContent[1 00];
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,caCont ent);
printf("%d\t%s\n",nId,caCo ntent);
}
return 0;
}
-Mahesh
#include <stdio.h>
int main()
{
char caTheLine[100],caContent[1
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)
if( caTheLine[0] == ';' ) continue;
sscanf(caTheLine,"%d$%s",&
printf("%d\t%s\n",nId,caCo
}
return 0;
}
-Mahesh
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks all.
I'll give allso some points to sunnycoder and cryptosid and brettmjohnson because it was my fault I misformulated my qyestion.
I'll give allso some points to sunnycoder and cryptosid and brettmjohnson because it was my fault I misformulated my qyestion.
ASKER
1$zfzdf
2$aadad
;ert
3$azdfadad
;4$44
5$6
6$azd
and I don't want to "convert" a file from the first form to the second...
(sorry, English isn't my native tongue).
So, I just need to adapt this piece of code:
--------
int dd;
char aaa[100];
while (!feof(Fp)) {
fscanf (Fp, "%d$%s",&dd, aaa);
if(dd!=0){printf("%d\t%s\n
Sleep(200);
}
-------
so that it reads the file, and only displays the lines that aren't comment.
(like in the first example)
Thanks all.