如何将一小段文本写入.txt
文件?我一直在谷歌搜索超过3-4个小时,但无法找到如何做到这一点.
fwrite();
有这么多论点,我不知道如何使用它.
当您只想为文件写一个名字和几个数字时,最简单的功能是什么.txt
?
编辑:添加了一段我的代码.
char name;
int number;
FILE *f;
f = fopen("contacts.pcl", "a");
printf("\nNew contact name: ");
scanf("%s", &name);
printf("New contact number: ");
scanf("%i", &number);
fprintf(f, "%c\n[ %d ]\n\n", name, number);
fclose(f);
Run Code Online (Sandbox Code Playgroud)
小智 251
FILE *f = fopen("file.txt", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
/* print some text */
const char *text = "Write this to the file";
fprintf(f, "Some text: %s\n", text);
/* print integers and floats */
int i = 1;
float py = 3.1415927;
fprintf(f, "Integer: %d, float: %f\n", i, py);
/* printing single chatacters */
char c = 'A';
fprintf(f, "A character: %c\n", c);
fclose(f);
Run Code Online (Sandbox Code Playgroud)
cpp*_*der 20
FILE *fp;
char* str = "string";
int x = 10;
fp=fopen("test.txt", "w");
if(fp == NULL)
exit(-1);
fprintf(fp, "This is a string which is written to a file\n");
fprintf(fp, "The string has %d words and keyword %s\n", x, str);
fclose(fp);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
564522 次 |
最近记录: |