#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc,char *argv[]){ /*(1)声明函数及变量*/ int writeToFile(char *str, char *fileName, char *mode); char str[100]; char fileName[] = "ex801.txt"; /*(2)获取键盘输入字串*/ fgets(str, 100, stdin); //gets(str); //将回车看作字串输入结束标志,字串中可以有空格 //scanf("%s", str); //将空格看作字串输入结束标志,字串中不能有空格 /*(3)将字串写入文件*/ int charNum = writeToFile(str, fileName, "w"); if(charNum < 0){ //printf("write error");//用于调试 return -1; } return 0; } /* * 函数名称:writeToFile * 函数功能:将字串写入文件 * 形式参数:char *str,一维字符数组(字符串)首地址 * 形式参数:char *fileName,待写入的文件路径及名称 * 形式参数:char *mode,文件使用方式 * 返 回 值:int型,若文件打开异常,返回 -1;否则返回写入到文件的字符数 */ int writeToFile(char *str, char *fileName, char *mode){ // 请编程实现本函数 }
时间: 2023-12-03 20:44:37 浏览: 26
以下是 `writeToFile` 函数的代码实现:
```c
int writeToFile(char *str, char *fileName, char *mode){
FILE *fp;
int charNum;
fp = fopen(fileName, mode);
if(fp == NULL){
printf("File open error!");
return -1;
}
charNum = fprintf(fp, "%s", str);
fclose(fp);
return charNum;
}
```
该函数的功能是将输入的字符串 `str` 写入文件 `fileName`,并返回写入到文件的字符数。其中,`mode` 参数表示文件使用方式,`"w"` 表示覆盖写入。
函数内部首先通过 `fopen` 函数打开文件,若打开失败则返回 -1。然后使用 `fprintf` 函数将字符串 `str` 写入文件,该函数返回写入到文件的字符数。最后使用 `fclose` 函数关闭文件。
相关问题
vs2008上,如何使用#include <tar.h> 例子
在 VS2008 中使用 `#include <tar.h>`,需要做以下几个步骤:
1. 下载 tar.h 头文件,可以从网上下载,比如在 GitHub 上可以找到该头文件的代码:https://github.com/panzi/uTAR/blob/master/tar.h
2. 将 tar.h 头文件保存在工程目录下的任意子目录中,比如 `include` 目录。
3. 在 VS2008 中打开工程,右键点击工程名称,选择“属性”,在弹出的对话框中选择“C/C++” -> “常规”,在“附加包含目录”中添加 tar.h 头文件所在目录的路径(比如 `$(ProjectDir)\include`)。
4. 在需要使用 tar.h 头文件的源文件中,加入 `#include <tar.h>` 即可。
例如,下面是一个使用 tar.h 头文件的示例代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tar.h>
int main(int argc, char* argv[])
{
// 创建 tar 文件
struct posix_header header;
memset(&header, 0, sizeof(header));
strncpy(header.name, "test.txt", sizeof(header.name));
strncpy(header.mode, "0000644 ", sizeof(header.mode));
strncpy(header.uid, "0000010 ", sizeof(header.uid));
strncpy(header.gid, "0000010 ", sizeof(header.gid));
strncpy(header.size, "00000000005 ", sizeof(header.size));
strncpy(header.mtime, "00000000000 ", sizeof(header.mtime));
memset(header.chksum, ' ', sizeof(header.chksum));
strncpy(header.typeflag, "0", sizeof(header.typeflag));
strncpy(header.linkname, "", sizeof(header.linkname));
strncpy(header.magic, TMAGIC, sizeof(header.magic));
strncpy(header.version, TVERSION, sizeof(header.version));
strncpy(header.uname, "", sizeof(header.uname));
strncpy(header.gname, "", sizeof(header.gname));
strncpy(header.devmajor, "", sizeof(header.devmajor));
strncpy(header.devminor, "", sizeof(header.devminor));
printf("Header checksum: %s\n", make_checksum(&header));
return 0;
}
```
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { printf("25600"); return 0; }
这段代码是一个简单的C语言程序,它的作用是输出数字25600。在程序中,使用了两个头文件<stdio.h>和<stdlib.h>,其中<stdio.h>包含了输入输出函数,而<stdlib.h>包含了一些常用的函数,例如atof()和atoi()。在main函数中,使用了三个参数,分别是argc、argv[]和return 0。其中,argc表示传递给程序的参数个数,argv[]表示传递给程序的参数列表,而return 0表示程序正常结束。在这个程序中,由于没有使用传递给程序的参数,因此argc和argv[]没有被使用到。