【发布时间】:2014-12-12 12:58:49
【问题描述】:
我的 main.c 文件中有这两个函数,但是当我尝试编译时出现以下错误:
main.c: In function ‘main’:
main.c:30: warning: assignment makes pointer from integer without a cast
main.c: At top level:
main.c:51: error: conflicting types for ‘getFileString’
main.c:30: note: previous implicit declaration of ‘getFileString’ was here
我不明白为什么我不能将指针返回到我在 getFileString 方法中创建的字符串。我真的需要一个解释。
我真的很困惑为什么会发生这种情况,任何帮助将不胜感激!
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include "tokenizer.h"
int main(int argc, char** argv){
if(argc != 3){
printf("not valid # of arguments");
return 1;
}
struct stat info;
int status;
status = stat(argv[2], &info);
if(status != 0){
printf("Error, errno = %d\n", errno);
return 1;
}
//command line argument is file
if(S_ISREG (info.st_mode)){
printf("%s is a file \n", argv[2]);
char *string1;
string1 = getFileString(argv[2]);
printf("string in file is %s", string1);
free(string1);
return 0;
}
if(S_ISDIR(info.st_mode)){
printf("%s is a directory \n", argv[2]);
openDirRec(argv[2]);
//what to do if command line argument is directory
}
return 0;
/*
DIR* directory;
struct dirent* a;
//file to write results to
FILE *newFile = fopen("results.txt", "w+");
*/
}
char* getFileString(char *fileName){
FILE* qp;
qp = fopen(fileName, "r");
char ch;
int sizeCheck = 0;
while((ch=fgetc(qp))!=EOF){
sizeCheck++;
}
fclose(qp);
if(sizeCheck == 0){
return NULL;
}
else{
char *fileString;
fileString = malloc(sizeof(char) * sizeCheck + 1);
FILE *cp;
cp = fopen(fileName, "r");
char cj;
int count = 0;
while((cj=fgetc(cp)!=EOF)){
fileString[count] = cj;
count++;
}
fileString[sizeCheck + 1] = '\0';
return fileString;
}
}
【问题讨论】: