c写文件

方法一:
#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>

void xprintf(const char *pszFormat, ...)
{
 char buf[1024] = {0};
 FILE *cfPtr = fopen("1.log", "a");
 va_list arg_ptr;
 va_start(arg_ptr, pszFormat);
// void va_start( va_list arg_ptr , prev_param );   (ANSI version)
 vsprintf(buf, pszFormat, arg_ptr);
//Write formatted output using a pointer to a list of arguments.
//int vsprintf( char * buffer , const char * format , va_list argptr ); 
 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语言中,可以使用标准库中的文件操作函数来进行文件。常用的文件操作函数有: 1. fopen():打开一个文件,返回文件指针。 2. fclose():关闭一个文件。 3. fgetc():从文件中读取一个字符。 4. fgets():从文件中读取一行。 5. fputc():向文件入一个字符。 6. fputs():向文件入一行。 7. fprintf():向文件中按指定格式入数据。 8. fscanf():从文件中按指定格式读取数据。 下面是一个简单的示例程序,演示了如何使用文件操作函数进行文件: ```c #include <stdio.h> int main() { FILE *fp; char c; // 打开文件 fp = fopen("test.txt", "w"); // 入数据 fputs("Hello, world!", fp); // 关闭文件 fclose(fp); // 打开文件 fp = fopen("test.txt", "r"); // 读取数据 while ((c = fgetc(fp)) != EOF) { printf("%c", c); } // 关闭文件 fclose(fp); return 0; } ``` 在这个示例程序中,首先使用 fopen() 函数打开一个名为 test.txt 的文件,并以入模式打开。然后使用 fputs() 函数向文件入一行数据。接着使用 fclose() 函数关闭文件。 然后再次使用 fopen() 函数打开同一个文件,但这次是以只读模式打开。然后使用 fgetc() 函数逐个读取文件中的字符,并使用 printf() 函数打印出来。最后使用 fclose() 函数关闭文件。 需要注意的是,在使用 fopen() 函数打开文件时,需要传入两个参数:文件名和文件打开模式。常见的文件打开模式有: 1. "r":只读模式。 2. "w":入模式,会覆盖原有文件。 3. "a":追加模式,不会覆盖原有文件,而是在文件末尾追加数据。 4. "rb"、"wb"、"ab":二进制文件模式,用于处理二进制文件。 更多文件操作函数的使用方法,请参考C语言标准库的相关文档。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值