【发布时间】:2013-10-18 15:15:26
【问题描述】:
我需要读取一个包含我的名字的 txt 文件,然后创建一个新的 txt 文件,其中包含我的名字但拼写倒过来,即(John Doe 变成 Doe,John)。我的任务说我可能需要创建一个临时数组来存储更改的 txt。
我收到警告:内置函数“strchr”错误的隐式声明不兼容。我会将它包含在代码中,这样您就可以确切地看到我收到此警告的位置。
这是我的代码,我感觉好像很接近了。我在这里做错了什么?请帮帮我。
#include <stdio.h>
int main (void)
{
FILE* txtFile=NULL;
txtFile=fopen("myName.txt", "r");
char myName [50]={'\0'};
if(txtFile==NULL)
{
printf("Failed to open file\n");
}
else
{
fgets(myName, sizeof(myName), txtFile);
printf("%s\n", myName);
}
FILE* newTxtFile=NULL;
newTxtFile=fopen("myNewName.txt", "w+");
char newName[50]={'\0'};
if(newTxtFile==NULL)
{
printf("Failed to open file\n");
}
else
{
fgets(newName, sizeof(newName), newTxtFile);
fprintf(txtFile, "%s", newName);
rewind(newTxtFile);
//
char * space;
char *first=NULL;
char *last = NULL;
char *firstspace;
char *name=NULL;
name = myName;
//incompatible implicit declaration of built-in function 'strchr'
firstspace=space=strchr(name,' ');
*firstspace='\0';
while (space!=NULL)
{
last = space+1;
space=strchr(space+1,' ');
}
printf("%s %s", last, name);
*firstspace=' ';
//
printf("text: %s \n", newName);
}
fclose(txtFile);
return 0;
}
【问题讨论】:
-
wrong的作用是什么? -
@JustinJasmann 内置函数'strchr'错误的隐式声明不兼容。
-
在哪里?如果有回溯,在您的帖子中包含此信息会很有用
-
我确实将它添加到代码中
-
您需要
#include <string.h>为strchr。