使用文件表示法和命令行语句,从文件中获取输入并将其保存到另一个文件(C)中。无法获取输出
我正在编写一个程序,使用文件表示法和命令行语句从文件中获取输入,将输入填充到结构中,然后将输入保存到另一个文件中。我也有一个排序函数,但我忽略了它,因为我认为它与我的问题无关。这是我第一次使用文件表示法,我无法在输出文件中获得任何输出。你知道为什么吗?我什么都试过了,但都没用。谢谢你的帮助使用文件表示法和命令行语句,从文件中获取输入并将其保存到另一个文件(C)中。无法获取输出,c,file,command-line,C,File,Command Line,我正在编写一个程序,使用文件表示法和命令行语句从文件中获取输入,将输入填充到结构中,然后将输入保存到另一个文件中。我也有一个排序函数,但我忽略了它,因为我认为它与我的问题无关。这是我第一次使用文件表示法,我无法在输出文件中获得任何输出。你知道为什么吗?我什么都试过了,但都没用。谢谢你的帮助 /* structure declaration */ struct personCatalog { char name[50]; char address[50]; char cit
/* structure declaration */
struct personCatalog {
char name[50];
char address[50];
char cityState[50];
char zipCode[7];
} ;
/* function prototypes */
void getPerson (struct personCatalog *arrayOfPointers[], int maxNumberOfPeople, FILE *inputFile);
void sortPerson (struct personCatalog *arrayOfPointers[]);
void putPerson(struct personCatalog *arrayOfPointers[], FILE *outputFile);
int main(int argc, const char * argv[]) {
int maxNumberOfPeople = 51;
struct personCatalog *pointerArray[maxNumberOfPeople];
/* add file streaming*/
FILE *inFile, *outFile;
inFile = fopen("/Users/myName/Desktop/inputText.txt", "r");
/* add command line statements */
if (( inFile = fopen(argv[1], "r")) == NULL) {
printf("Can't open the input file");
exit(1);
}
getPerson(pointerArray, maxNumberOfPeople, inFile);
sortPerson(pointerArray);
outFile = fopen("/Users/myName/Desktop/outputText.txt", "w");
if ((outFile = fopen(argv[2], "w")) == NULL) {
printf("Can't open the output file");
exit(1);
}
putPerson(pointerArray, outFile);
fclose(inFile);
fclose(outFile);
return 0;
}
/* function to fill the structures */
void getPerson (struct personCatalog *arrayOfPointers[], int maxNumberOfPeople, FILE *inputFile){
struct personCatalog *tempPointer;
int buffer = 512;
char stringCollector[buffer];
int num = 0;
while ((num < maxNumberOfPeople) && (fgets(stringCollector, buffer, inputFile) != NULL) ) {
/* dynamic memory allocation */
tempPointer = (struct personCatalog *) malloc(sizeof(struct personCatalog));
/* filling the structures */
strcpy(tempPointer->name, stringCollector);
fgets(tempPointer->address, buffer, inputFile);
fgets(tempPointer->cityState, buffer, inputFile);
fgets(tempPointer->zipCode, buffer, inputFile);
arrayOfPointers[num] = tempPointer;
num++;
}
/* adding a null character to the end of the pointer array */
arrayOfPointers[num] = '\0';
}
/* print function */
void putPerson(struct personCatalog *arrayOfPointers[], FILE *outputFile){
int num = 0;
while (arrayOfPointers[num] != NULL) {
printf("------Person #%d------\n", num);
fputs(arrayOfPointers[num]->name, outputFile);
fputs(arrayOfPointers[num]->address, outputFile);
fputs(arrayOfPointers[num]->cityState, outputFile);
fputs(arrayOfPointers[num]->zipCode, outputFile);
/* memory free */
free(arrayOfPointers[num]);
num++;
}
}
/*结构声明*/
结构personCatalog{
字符名[50];
字符地址[50];
半胱氨酸盐[50];
char-zipCode[7];
} ;
/*功能原型*/
void getPerson(struct personCatalog*arrayOfPointers[],int maxNumberOfPeople,FILE*inputFile);
void sortPerson(struct personCatalog*arrayOfPointers[]);
void putPerson(struct personCatalog*arrayOfPointers[],FILE*outputFile);
int main(int argc,const char*argv[]{
int maxNumberOfPeople=51;
struct personCatalog*pointerArray[maxNumberOfPeople];
/*添加文件流*/
文件*inFile,*outFile;
infle=fopen(“/Users/myName/Desktop/inputText.txt”,“r”);
/*添加命令行语句*/
if((infle=fopen(argv[1],“r”)==NULL){
printf(“无法打开输入文件”);
出口(1);
}
getPerson(pointerArray、maxNumberOfPeople、infle);
分拣员(点阵列);
outFile=fopen(“/Users/myName/Desktop/outputText.txt”,“w”);
if((outFile=fopen(argv[2],“w”)==NULL){
printf(“无法打开输出文件”);
出口(1);
}
putPerson(点阵列、出文件);
fclose(infle);
fclose(输出文件);
返回0;
}
/*函数填充结构*/
void getPerson(结构personCatalog*arrayOfPointers[],int-maxNumberOfPeople,FILE*inputFile){
结构personCatalog*临时指针;
int缓冲区=512;
字符字符串收集器[缓冲区];
int num=0;
while((numname,stringCollector);
fgets(临时指针->地址、缓冲区、输入文件);
fgets(tempPointer->cityState、缓冲区、输入文件);
fgets(临时指针->zipCode、缓冲区、输入文件);
arrayOfPointers[num]=临时指针;
num++;
}
/*将空字符添加到指针数组的末尾*/
数组指针[num]='\0';
}
/*打印功能*/
void putPerson(struct personCatalog*arrayOfPointers[],FILE*outputFile){
int num=0;
while(arrayOfPointers[num]!=NULL){
printf(“----人#%d----\n”,num);
fputs(arrayOfPointers[num]->name,outputFile);
fputs(arrayOfPointers[num]->地址,outputFile);
fputs(arrayOfPointers[num]->cityState,outputFile);
fputs(arrayOfPointers[num]->zipCode,outputFile);
/*无内存*/
自由(数组指针[num]);
num++;
}
}
主屏幕中的正确代码
int main(int argc, const char * argv[]) {
int maxNumberOfPeople = 51;
struct personCatalog *pointerArray[maxNumberOfPeople];
/* add file streaming*/
FILE *inFile, *outFile;
if (argc != 3) {
printf("Usage: /Users/myName/Library/Developer/Xcode/DerivedData/progarmName-cglrwvyyabrneocwtllxqdhygaaj/Build/Products/Debug/progarmName /Users/myName/Desktop/inputText.txt /Users/myName/Desktop/outputText.txt\n");
exit(1);
}
/* add command line statements */
if (( inFile = fopen(argv[1], "r")) == NULL) {
printf("Can't open the input file");
exit(1);
}
getPerson(pointerArray, maxNumberOfPeople, inFile);
sortPerson(pointerArray);
if ((outFile = fopen(argv[2], "w")) == NULL) {
printf("Can't open the output file");
exit(1);
}
putPerson(pointerArray, outFile);
fclose(inFile);
fclose(outFile);
return 0;
}
我不明白你所说的“文件表示法”是什么意思,或者问题是什么。文件表示法是“file*inputFile,*outputFile”。问题是我无法在我想要的文件中获得任何输出。我不知道为什么。你做过任何调试吗?那么你实际上并没有运行你认为正在运行的代码。我不是要指出明显的原因,但你泄漏任何
文件*
sinfle
和outFile
引用并打开argv[1]
和argv[2]
在它们上面的正上方?当argv[2]恰好也是“/Users/myName/Desktop/outputText.txt”
时会发生什么?我首先要清理你泄漏的文件*
s。