c写文件
来源:互联网 发布:如何用爬虫爬数据 编辑:程序博客网 时间:2024/05/21 09:46
方法一:
#include<stdio.h>
main()
{
FILE *fp;
int i=0;
char *s="Am I right?";
fp=fopen("c://text.txt","wr");
while(*s)
{ printf("%c",*s);
fseek(fp,i++,SEEK_SET);
fprintf(fp,"%c",*s++); //++优先级高于*
}
fclose(fp);
getchar();
}
方法二:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
main()
{
FILE *fp;
char ch;
fp = fopen("F://file.txt","wb"); //写的方式打开文件
if( fp == NULL)
{ printf("can't open file.txt/n"); exit(1); } //打开文件失败则退出
scanf("%c",&ch);
while(ch != '*') //输入字符*则退出
{
printf("%c/n",ch);
fputc(ch,fp); //将ch写入你打开的文件
fflush(stdin); //清除输入缓冲区
scanf("%c",&ch); //继续输入一个字符
}
printf("input over!/n");
getch();
}
方法三:
#include <stdio.h>
#include <stdarg.h>
{
char buf[1024] = {0};
FILE *cfPtr = fopen("1.log", "a");
va_list arg_ptr;
va_start(arg_ptr, pszFormat);
vsprintf(buf, pszFormat, arg_ptr);
va_end(arg_ptr);
printf("%s", buf);
if (cfPtr!=NULL)
{
fprintf(cfPtr, "%s", buf);
fclose(cfPtr);
}
}
int main(int argc, char *argv[])
{
xprintf("%-10d%s/n", 99, "hello world");
return 0;
}
方法四:
void WriteDat(void)
{
int i;
char xx[2][23]={"You He Me","I am a student."};//测试数据.
FILE *fp;
fp=fopen("OUT6.txt","a+");
for(i=0;i<2;i++)
{
printf("%s/n",xx[i]);//运行时在屏幕上显示
fprintf(fp,"%s/n",xx[i]);//写入文件,
}
fclose(fp);
}
方法五:
手动可以用重定向
a.out > out.txt
程序实现可以用fprintf
#include<stdio.h>
main()
{
FILE *fp;
int i=0;
char *s="Am I right?";
fp=fopen("c://text.txt","wr");
while(*s)
{ printf("%c",*s);
fseek(fp,i++,SEEK_SET);
fprintf(fp,"%c",*s);
s++;
}
fclose(fp);
getchar();
}
- 写文件(C/C++)
- c写文件
- C语言写文件
- 写C文件
- C#streamwriter写文件
- C写日志文件
- c#---写文件
- c写文件
- C语言 写文件操作
- 写 C 语言 头文件
- 用C 写html文件
- C代码写文件demo
- 【C++】c++写Excel文件
- Linux C写日志文件
- C或者C+怎么写可以文件关联文件
- 写好C语言的头文件
- c 写cgi 上传文件代码
- 写好C语言的头文件
- 高质量C++/C编程指南 -- 第1章 文件结构
- Dev-C++ 安装&使用教程
- 配置IBM WEBSPHERE MQ触发器
- 什么是 Domino 分区服务器?
- 高质量C++/C编程指南 -- 第2章 程序的版式
- c写文件
- 高质量C++/C编程指南 -- 第3章 命名规则
- 自动更换IP
- 在ASP.NET中跨页面实现多选
- 高质量C++/C编程指南 -- 第4章 表达式和基本语句
- 什么是Kernel、Shell、Bash 很幽默的解释
- 专家点评如何让你的ERP变得更省钱?
- 高质量C++/C编程指南 -- 第5章 常量
- XMLNode类