F打開和FCLOSE都在我的源文件包函數打開文件時錯誤檢查。當我運行我的程序時,它說有一個Fopen錯誤。我在文件打開時看不到任何錯誤的原因。我怎樣才能從打開文件中找到錯誤?
很抱歉的長碼。
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "perry.h"
int main(void)
{
void copyStrings(char *infile, char *outfile, char ch);
void compareFiles(char *infile, char *outfile);
char inputfile[80];
char outputfile[80];
char ch;
printf("Enter input filename: ");
scanf("%s", inputfile);
printf("Enter output filename: ");
scanf("%s", outputfile);
printf("Enter a character: ");
scanf(" %c", &ch);
if(!isalpha(ch))
{
printf("Did not enter a letter!");
exit(1);
}
copyStrings(inputfile, outputfile, ch);
compareFiles(inputfile, outputfile);
return 0;
}
void copyStrings(char *infile, char *outfile, char ch)
{
int count;
char *ptr;
char *line;
char linePart[80];
FILE *fin;
FILE *fout;
fin = Fopen(infile, "r");
fout = Fopen(outfile, "w");
while(fgets(line, 80, fin) != NULL)
{
for(ptr=line;ptr<line+strlen(line);ptr++)
{
if(*ptr == ch)
count ++;
}
if(count < 2)
fputs(line, fout);
else
{
memset(linePart, '\0', strlen(line)+1);
line = strchr(line, ch);
strncpy(linePart, line, strchr(line+1, ch) - line + 1);
fputs(linePart, fout);
fprintf(fout, "\n");
}
}
Fclose(fout);
Fclose(fin);
return;
}
void compareFiles(char *infile, char *outfile)
{
int count = 0;
char inputString[80];
char outputString[80];
FILE *fin;
FILE *fout;
fin = Fopen(infile, "r");
fout = Fopen(outfile, "r");
while(fgets(inputString, 80, fin) != NULL)
{
count += 1;
if(strcmp(inputString, fgets(outputString, 80, fout)) == 0)
{
printf("Strings are equal at line %d\n\nBoth strings look like this: %s",
count, inputString);
}
}
Fclose(fout);
Fclose(fin);
return;
}
看到了`Fopen`實際的代碼將很長的路要走爲回答這個問題。 – shoosh 2011-02-02 01:03:03