开发者

How would I use fgets to not read in spaces

I am comparing two files line by line and was wondering if there was a way to get fgets not to read in spaces. For example, if one file has

  hello world

and another file had

hello       w开发者_StackOverfloworld

I want to ignore the first two spaces and the spaces in the middle and return that both these lines are equal.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define BUFSIZE 1024 


int linecmp(FILE *fp1, FILE *fp2);
int charcmp(FILE *fp1, FILE *fp2);
int wordcmp(FILE *fp1, FILE *fp2);

int main(int argc, char * argv[])
{
size_t i;
FILE *fp1;
FILE *fp2;

fp1 = fopen("input.txt", "rb+");
fp2 = fopen("input2.txt", "rb+");



printf("%d",linecmp(fp1, fp2));


return 0;
}

int linecmp(FILE *fp1, FILE *fp2)
{
char line[BUFSIZE];
char line2 [BUFSIZE];
size_t linecount = 0;
size_t linecount2 = 0;

/*reads from first file pointer*/
while(fgets(line,BUFSIZE, fp1))
{   
    ;
}
/*reads from second file pointer*/
while(fgets(line2,BUFSIZE, fp2))
{
    ;
}
return 0;
}


No, the fgets function has no such capability. You have to remove the spaces yourself.

But perhaps not all the spaces. For the purpose of your assignment, should "hello world" be considered a match with "helloworld"? If not, you might need to remove all leading and trailing whitespace, but treat interior whitespace with a bit more care -- perhaps replacing a run of whitespace characters with a single blank before doing the comparison.


How about using scanf() sscanf() with "%s" to skip whitespace ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜