How to verify a four-digit already exists in a txt file

Category: C/C++ -> C Author: xiangjing19910907 Date: 2006-01-07 17:41:06
 
xiangjing19910907
2006-01-07 17:41:06
in.txt where is an arbitrary 4 4 digits but they are separated by commas 1234,2345,3456,4567 last four digits Lane did not enter a comma now 4 digits 4 digits determine whether this exists in in.txt li

FILE * in;
int c;
int num;
scanf ("% d", & c);
while ((fscanf (in, "% d", & num)) =! EOF)
{
if (c = num)
{printf ("right");}
else
{printf ("wrong";}
}
in.txt inside is 1234,2345,3456,4567 final a 4-digit behind the comma
not only recognize the first four digits 1234 I do not know how to do the identification comma added% d in front of you? front back I have tried not work ah
or should for the first four digits turn it verified once? but also how to make the second fscanf 4 digits from the second started yet?
lixiaowang
2006-01-07 17:44:00
#include <stdlib.h>
#include <stdio.h>

int main()
{
FILE *in=NULL;

if( ( in=fopen("d:\\text.txt","r") )==NULL )
{
return -1;
}

int get,find;

scanf("%d",&find);

while(true)
{
if( fscanf(in,"%d",&get)!=EOF )
{
if(get==find)
{
printf("right\n");
}
}
else
{
break;
}

if( fgetc(in)==EOF )
{
break;
}
}


fclose(in);

return 0;
}
zhangby1237
2006-01-07 18:01:17
previous three with fscanf (in, "% d,", & num)
last fscanf (in, "% d", & num)
qinxvan
2006-01-07 18:14:34
Can the first time with "% d" behind the use ", % d "it? because even back to Riga in.txt many random 4-digit
gcckobe
2006-01-07 18:30:33

possible..
hanxujason
2006-01-07 18:38:08
Why do not first read 4. then once sentenced, as long as there is a non-compliance on over the
sdshaoshuai
2006-01-07 18:51:33
Since only four numbers, you can also use an array of bars
Michael_Z1985Y
2006-01-07 19:03:07
2nd floor of the amount of the situation as also added a lot of talk behind random four-digit
another kind of want to break pause command I do not know how to use How to use normal printf scanf or take them to back it ?
owanwaneryio
2006-01-07 19:05:12
FILE * in;
int c;
int num;
char numstr[10];
char *p;
scanf("%d",&c);
while((fscanf(in, "%s",numstr))!=EOF)// {
  if (p=strrchr(numstr, ',')) *p=0; // ','
  num=atoi(numstr);
  if (c==num) //if (c=num)
  { printf("right ");}
  else  
  { printf("wrong ");}//{ printf("wrong";}
}
limingshanshan
2006-01-07 19:19:22
fourth floor of positive solutions, the landlord, the code you have questions, each lookup if the number is not found on the wrong, Found this number is printed right, so there is some wrong or right, this and the comma does not matter, because scanf% d function is non-numeric character as a delimiter.
elec8com
2006-01-07 19:22:42
for reference only
//d.txt :(  // )
//01.2,3.45;5.6789 -0.12345678901234 abc 1234567890.123456789123456789 1e300 0
#include <stdio.h>
int n,r;
double d;
FILE *f;
void main() {
    f=fopen("d.txt","r");
    n=0;
    while (1) {
        r=fscanf(f,"%lf",&d);
        if (1==r) {
            n++;
            printf("[%d]==%.15lg\n",n,d);//
        } else if (0==r) {
            fgetc(f);
        } else break;
    }
    fclose(f);
}
// :(  // )
//[1]==1.2
//[2]==3.45
//[3]==5.6789
//[4]==-0.12345678901234
//[5]==1234567890.12346
//[6]==1e+300
//[7]==0

smhsoft
2006-01-07 19:42:14


you take it for granted, you try to run a direct% d does not, I write to find the right exit on print, you change what you can.
lk7262123
2006-01-07 19:58:22
So are you asking in Baidu ah, huh, huh.
ben_bamboo
2006-01-07 20:00:26
landlord know the way, just use a problem, this would much practiced, there is no way
zxx2000hl
2006-01-07 20:09:38

perfect look, this method after the comma must have whitespace

#include <cstdio>
#include <cstring>
#include <cstdlib>

int main()
{
FILE * in;
int c;
int num;
char str[128];
char *p;
if( ( in = fopen("file.txt","r") ) == NULL )
return -1;
scanf("%d",&c);
while((fscanf(in, "%s", str)) != EOF)// 
{
if (p = strrchr(str, ',')) 
*p=0;
num = atoi(str);
printf("%d ", num);
if (c == num) 
{
printf("right ");
break;
}
}
return 0;
}
/*
file.txt :
1234, 234, 44, 124, 542,  442, 135
212, 42, 11, 33,  233, 100, 123


1234 234 44 124 542 442 135 212 42 11 33 233 100 right
*/
kaokobe
2006-01-07 20:26:13
while ( ! feof (in))
phrase can
while (fscanf (in, "% d,", & c)! = EOF) and the like instead of you?
I did not learn this instruction book feof exercise requirements can not be learned commands...