【问题标题】:Reading then rewriting and modifying txt files读取然后重写和修改txt文件
【发布时间】: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 &lt;string.h&gt;strchr

标签: c string text


【解决方案1】:

您的代码中有很多无用的垃圾。

您的新文件没有任何内容的原因是您在之前的文件中再次写入新数据

看这里:

fprintf(txtFile, "%s", newName);



#include <stdio.h>
#include <string.h>

int main (void)
{
FILE* txtFile=NULL;

txtFile=fopen("helloWorld.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[200]={'\0'};


if(newTxtFile==NULL)
{
    printf("Failed to open file\n");
}
else
{
fgets(newName, sizeof(newName), newTxtFile);


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);
/* my changes start here*/
strcat(newName,last);


strcat(newName," ");


strcat(newName,name);

printf("%s", newName);

fprintf(newTxtFile, "%s", newName);






}
fclose(txtFile);
fclose(newTxtFile);

return 0;
}

【讨论】:

  • 唯一的问题是我不需要倒着拼写名字,我需要先用姓氏然后名字拼写名字。例如 John Doe 变成 Doe, John
  • @Jongware LOL 是的,我现在看到了。我希望 SO 对评分
【解决方案2】:

首先,你需要

你处理输出文件的方式有点奇怪。

你应该打开它来输出 ("w");

删除这 3 行:

fgets(newName, sizeof(newName), newTxtFile);

fprintf(txtFile, "%s", newName);

rewind(newTxtFile);

然后添加一行以将输出打印到屏幕上打印位置旁边的新文件中:

fprintf(newTxtFile, "%s, %s", last, name); 

最后,在开头添加

#include <string.h>

获取strchr 的原型。

应该这样做!

【讨论】:

  • 它并没有将编辑后的txt保存到新文件中
  • 我的 fprintf 试图打印到输入文件。我已经更正了我的答案。
猜你喜欢
  • 2017-11-19
  • 2021-06-16
  • 2010-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多