我对编程相当陌生,这是我在这里的第一个问题,希望我不会违反任何(太多)规则!我通常喜欢自己想办法解决问题,但我很困惑,所以就这样说:
我的计算机科学II课程有一个作业,要求我在一个文件中创建/写我的名字(比如John ),然后输出该文件的内容。这部分我已经完成了。
现在,作业的后半部分告诉我要编写和使用一个函数,将我的名字转换为姓、名格式,并将其写到一个单独的文件中。它并没有具体地告诉我输出该文件的内容,但我无论如何都想输出。
我不太清楚如何转换这个名字。我是否应该为我的名字和姓氏创建两个不同的字符串(我曾考虑过这样做,但它似乎并不特别有效)?它似乎也不像需要使用结构的情况,但我可能(也可能是)完全放弃了。到目前为止,我的情况如下:
#include <stdio.h>
#define SIZE 20
int main(void)
{
FILE *readPtr;//readfile.txt file pointer
FILE *writePtr;//writefile.txt file pointer
char nameFull[SIZE] = "John Doe";
readPtr = fopen("readfile.txt", "w");//creates readfile.txt file for writing
fprintf(readPtr, "%s", nameFull);//writes contents of string "nameFull" to file
fclose(readPtr);//close file
if((readPtr = fopen("readfile.txt", "r")) == NULL){//returns error message if file can't be opened
printf("Error opening file!\n");
}
else{
while(!feof(readPtr)){//end of file check for readPtr file
fscanf(readPtr, "%[^\n]s", nameFull);
printf("\n%s\n\n", nameFull);
}
fclose(readPtr);
}
}如有任何答复或建议,敬请见谅。哪怕只是往正确的方向推一下就好了。此外,对我的职位的任何建议/批评也是受欢迎的,这样我就可以改进它们了。耽误您时间,实在对不起!
发布于 2016-03-23 22:22:42
首先,您应该在一个单独的函数中将" FN“转换为"LN,FN”的代码。函数必须分配内存,因为LN,FN表单包含另外两个字符(逗号和空格)。
char *revname(const char *fnln)
{
char *buf;
/* +2 for space and comma, +1 for null terminator */
if ((buf = malloc(strlen(fnln) + 3)) != NULL) {
char *p, *q;
size_t len;
/* find the first space in the string */
if ((p = strchr(fnln, ' ')) != NULL) {
/* copy the text after the space */
strcpy(buf, p + 1);
strcat(buf, ", ");
len = strlen(buf);
/* append the first name */
for (q = (char *) fnln; q < p; ++q)
buf[len++] = *q;
}
/* null terminate the string */
buf[len] = '\0';
}
return buf;
}现在,主要代码。while (!feof())是错误的,因为EOF指示符在读取失败后被设置为,而不是以前。因此,循环将比预期的运行一次。完整的代码是
void remlast(char *str) { str[strlen(str) - 1] = '\0'; }
int main()
{
FILE *fp;
char name[20], *newname;
if ((fp = fopen("filename", "w")) != NULL) {
/* write the name onto the file */
fputs("John Doe", fp);
/* reuse the same file pointer for reading */
if ((fp = freopen("filename", "r", fp)) != NULL) {
fgets(name, 20, fp);
/* remove the trailing newline from fgets */
remlast(name);
newname = revname(name);
printf("%s\n", newname);
/* free memory allocated by revname */
free(newname);
fclose(fp);
} else {
perror("freopen");
return 1;
}
} else {
perror("fopen");
return 1;
}
return 0;
}注意:我还没有测试过main,所以如果有bug,请告诉我。
发布于 2016-03-24 03:07:56
这里有一种方法。
是的,这是一个小的“欺骗”在结尾与简单的元素反向,虽然一旦读取到名称的顺序可以通过另一个函数,无论是互换或词汇或字长顺序或任何传递它的名称。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define EXIT_FAIL 0x01
#define MAX_WORD_LEN 0x1F
#define MAX_WORDS 0x02
int main(void)
{
FILE *fptr_MyFile;
unsigned char *ucName[MAX_WORDS];
unsigned char ucWord[MAX_WORD_LEN];
unsigned int uiW0;
ucName[0x00] = "John";
ucName[0x01] = "Doe";
printf("Name array %s %s\n\n", ucName[0x00], ucName[0x01]);
fptr_MyFile = fopen("FNLNnamefile.txt", "w");
if (fptr_MyFile == NULL)
{
printf("Could not create file. Ending.\n");
return(EXIT_FAIL);
}
fprintf(fptr_MyFile, "%s %s", ucName[0x00], ucName[0x01]);
fclose(fptr_MyFile);
if ((fptr_MyFile = fopen("FNLNnamefile.txt", "r")) == NULL)
{
printf("Could not create file. Ending.\n");
return(EXIT_FAIL);
}
ucName[0x00] = NULL;
ucName[0x01] = NULL;
printf("Name array %s %s\n\n", ucName[0x00], ucName[0x01]);
for (uiW0 = 0x00; ((uiW0 < MAX_WORDS) && (!feof(fptr_MyFile))); uiW0 = uiW0 + 0x01)
{
fscanf(fptr_MyFile, "%s", ucWord);
ucName[uiW0] = calloc(strlen(ucWord) + 1, sizeof(char));
strcpy(ucName[uiW0], ucWord);
}
fclose(fptr_MyFile);
printf("Name array %s %s\n\n", ucName[0x00], ucName[0x01]);
fptr_MyFile = fopen("LNFNnamefile.txt", "w");
if (fptr_MyFile == NULL)
{
printf("Could not create file. Ending.\n");
return(EXIT_FAIL);
}
fprintf(fptr_MyFile, "%s, %s", ucName[0x01], ucName[0x00]);
fclose(fptr_MyFile);
printf("Final result %s, %s\n", ucName[0x01], ucName[0x00]);
return(EXIT_SUCCESS);
}https://stackoverflow.com/questions/36189412
复制相似问题