我有这两个功能在我的main.c文件,但是当我尝试编译我收到以下错误: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;
}
}
谢谢!我还是一个在c的菜鸟,但它现在完美。 – atg963 2014-10-17 01:44:53
其实我有另一个小问题,以及当字符串打印出来的命令行即时通讯获得一堆奇怪的字符,看起来像盒子中的十六进制数字任何想法这可能是什么? – atg963 2014-10-17 01:50:10
@ atg963通常这意味着你正在尝试将某些东西显示为一个字符串,但它没有被初始化,因此它包含垃圾。 – cdhowie 2014-10-17 22:20:45