将数据输入到文件中

简介: 将数据输入到文件中
#include <stdio.h>
#include <stdlib.h>
void write();//声明函数
void read();
int main()//主函数
{
    write();//调用函数
    read();
    return 0;
}
void write()//实现函数
{
    FILE *fp;//文件指针
    if((fp = fopen("F:\\Codes\\Codeblocks\\C\\FileWriting\\simple.txt","ab+"))==NULL)//文件的路径
    {
        printf("Can not open this file!\n");//找不到文件时结束
        exit(0);
    }
    int i,x,n;//i为记录次数的值,x为输入的参数,n为输入的数的总个数
    printf("please input the number of integers:\n");//总共输入几个数
    scanf("%d",&n);
    printf("please input %d integer:\n",n);//输入这几个数
    for(i = 0; i < n; i++)
    {
        scanf("%d",&x);
        fprintf(fp,"\n");//换行
        fprintf(fp,"%d",x);//输出到文件中
    }
    fclose(fp);
}
void read()
{
    printf("the file looks as follows:\n");
    FILE *fp;//文件指针
    if((fp = fopen("F:\\Codes\\Codeblocks\\C\\FileWriting\\simple.txt","r"))==NULL)//文件的路径
    {
        printf("Can not open this file!\n");//找不到文件时结束
        exit(0);
    }
    char ch;
    ch = fgetc(fp);//获取文件中的字符
    while(!feof(fp))//判断是否到文件末尾
    {
        putchar(ch);//输出字符
        ch = fgetc(fp);
    }
    printf("\n");
    fclose(fp);
}
相关文章
|
1月前
|
存储 C++
C++系列五:输入/输出
C++系列五:输入/输出
|
1月前
|
C语言
输入&输出
【2月更文挑战第13天】输入&输出。
12 1
|
2月前
|
编译器 C++
【c++】C++输入&输出
【c++】C++输入&输出
【c++】C++输入&输出
|
8月前
|
存储
I/O输入和输出详解
I/O输入和输出详解
|
6月前
|
编译器 C语言
C 输入 & 输出
C 输入 & 输出。
75 1
|
8月前
|
C++
|
12月前
|
存储 程序员 C++
C++ 中的基本输入/输出
C++ 附带的库为我们提供了许多执行输入和输出的方法。在 C++ 中,输入和输出以字节序列或更通常称为流的形式执行。
102 0
|
C++
c++学习:输入信息和输出信息
c++学习:输入信息和输出信息
58 0
|
存储
输入功能介绍
输入功能介绍
133 0