导航:首页 > 编程语言 > 对文件追加信息的编程

对文件追加信息的编程

发布时间:2022-07-19 03:56:58

① c语言文件读写“追加”

通过一个简单的示例进行讲解(此示例用文本文件进行演示):

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#define FILE_NAME "demo.txt" // 文件名称

int main()

{

FILE* fp = NULL; // 文件指针

char* szAppendStr = "Text";

errno_t eResult;

// 以附加方式打开可读/写的文件, 如果没有此文件则会进行创建,然后以附加方式打开可读/写的文件

eResult = fopen_s(&fp, FILE_NAME, "a+");

// 打开文件失败

if (eResult != 0)

exit(-1);

// 将追加内容写入文件指针当前的位置

fputs(szAppendStr, fp);

// 最后不要忘了,关闭打开的文件~~~

fclose(fp);

return 0;

}

(1)对文件追加信息的编程扩展阅读

文件的打开和关闭

open()函数的作用是打开文件,其调用格式为: int open(char *filename, int access); 该函数表示按access的要求打开名为filename的文件,返回值为文件描述字,其中access有两部分内容: 基本模式和修饰符, 两者用" "("或")方式连接。修饰符可以有多个, 但基本模式只能有一个。

open()函数打开成功, 返回值就是文件描述字的值(非负值), 否则返回-1。 close()函数的作用是关闭由open()函数打开的文件, 其调用格式为: int close(int handle); 该函数关闭文件描述字handle相连的文件。

② c语言中怎样给已有文件追加数据

先用fopen打开,权限为读写,然后fseek把指针移到文件末尾,再往里写东西就行了,具体的函数用法参见msdn。要编程建议还是用好msdn

③ 对一个txt文件追加一段内容,用java实现

import java.io.File;import java.io.FileWriter;import java.io.Writer;public class Test{ public static void main(String args[]) throws Exception{ File f = new File("d:"+File.separator+"test.txt"); Writer out = null; out = new FileWriter(f,true) //true表示追加 String str = "\r\n你好\r\nHello World!"; out.writer(str); out.close(); }}

④ java如何对文件追加写入

java文件追加内容的三种方法:
方法一:
public static void writeToTxtByRandomAccessFile(File file, String str){
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(file,"rw");
long len = randomAccessFile.length();
randomAccessFile.seek(len);
randomAccessFile.writeBytes(new String(str.getBytes(),"iso8859-1")+"\r\n");
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法二:
public static void writeToTxtByFileWriter(File file, String content){
BufferedWriter bw = null;
try {
FileWriter fw = new FileWriter(file, true);
bw = new BufferedWriter(fw);
bw.write(content);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法三:
public static void writeToTxtByOutputStream(File file, String content){
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, true));
bufferedOutputStream.write(content.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e ){
e.printStackTrace();
}finally{
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

python怎么以追加的方式写文件

一、用Python创建一个新文件,内容是从0到9的整数, 每个数字占一行:

#python

>>>f=open('f.txt','w') # r只读,w可写,a追加

>>>for i in range(0,10):f.write(str(i)+' ')

. . .

>>> f.close()

二、文件内容追加,从0到9的10个随机整数:

#python

>>>import random

>>>f=open('f.txt','a')

>>>for i in range(0,10):f.write(str(random.randint(0,9)))

. . .

>>>f.write(' ')

>>>f.close()

三、文件内容追加,从0到9的随机整数, 10个数字一行,共10行:

#python

>>> import random

>>> f=open('f.txt','a')

>>> for i in range(0,10):

. . . for i in range(0,10):f.write(str(random.randint(0,9)))

. . . f.write(' ')

. . .

>>> f.close()

四、把标准输出定向到文件:

#python

>>> import sys

>>> sys.stdout = open("stdout.txt", "w")

⑥ 使用python编程,实现对txt文件中每行内容进行追加。


#-*-coding:utf-8-*-

importre
importos

filepath='E:\data11-20\0.025'
#filepath=os.getcwd()
lst=[]
foriinrange(3,100):
filename='plane1-conv{:03d}.out'.format(i)
fullname=(os.sep).join([filepath,filename])
withopen(fullname)asf:
s=f.read().strip()
lst1=[re.split(r's+',si.strip())[-1]forsiins.split(' ')]
lst.append(lst1)
#lst是一个二维数组,每个文件的最后一列作为一个一维数组存在里面
#然后找出最长列的长度lmax,其他比它短的数据列,用lmax-len(i)组空格补到和它一样长
#每组空格的数目等于数据列的第一个数据的长度
lmax=max([len(i)foriinlst])
ws=[i+[''*len(i[0])]*(lmax-len(i))foriinlst]

withopen('E:\hehe.txt','w')aswf:
wf.write(' '.join([''.join(i)foriinws]))

⑦ 如何给文件追加信息(必追加40分)

楼主,你的代码里面
scanf();
fscanf();
的参数都有错误,你好好看看就会发现有2个income
我直接改了,写了一个测试的控制台,你运行看看吧

#include<stdio.h>
#include<stdlib.h>
#define SIZE 2
struct student_type
{
int num;
char mima[20];
char name[100];
int income;
int payout;
int total;
}stud[SIZE];
FILE *fp;
int i;
int choice;

void menu()
{
system("cls");
printf("\n\n\n\t\t\t主菜单\n\n");
printf("\t\t\t1.把数据保存为文件\n\n");
printf("\t\t\t2.读入保存文件\n\n");
printf("\t\t\t3.追加用户\n\n");
printf("\t\t\t4.返回录入数据\n\n");
printf("\t\t 请输入你的选择:");
scanf("%d",&choice);
}

void input()
{
for(i=0;i<SIZE-1;i++)
{
printf("请输入一个号码:\n");
fflush(stdin);
scanf("%d",&stud[i].num);
fflush(stdin);
printf("请输入密码:\n");
fflush(stdin);
scanf("%s",stud[i].mima);
printf("请输入姓名:\n");
fflush(stdin);
scanf("%s",stud[i].name);
printf("请输入收入:\n");
fflush(stdin);
scanf("%d",&stud[i].income);
printf("请输入支出:\n");
fflush(stdin);
scanf("%d",&stud[i].payout);
printf("总收入:\n");
fflush(stdin);
scanf("%d",&stud[i].total);
}
}

void file_save(struct student_type *stud)
{
fp=fopen("stu_list5.txt","w");
for(i=0;i<SIZE-1;i++)
{
fprintf(fp,"%d %s %s %d %d %d\n",stud[i].num,stud[i].mima,stud[i].name,stud[i].income,stud[i].payout,stud[i].total);
}
printf("文件保存成功!\n");
system("pause");
fclose(fp);
}

void file_read()
{
if((fp=fopen("stu_list5.txt","r"))==NULL)
{
printf("文件打开失败!");
exit(0);
}
for(i=0;!feof(fp)&&i<SIZE-1;i++)
{
fscanf(fp,"%d %s %s %d %d %d",&stud[i].num,stud[i].mima,stud[i].name,&stud[i].income,&stud[i].payout,&stud[i].total);
printf("%d %s %s %d %d %d\n",stud[i].num,stud[i].mima,stud[i].name,stud[i].income,stud[i].payout,stud[i].total);
}
system("pause");
fclose(fp);
}

void file_rwrite()
{
if((fp=fopen("stu_list5.txt","a"))==NULL)
{
printf("文件打开失败!");
exit(0);
}
for(i=1;i<SIZE;i++)
{
printf("请输入一个号码:\n");
fflush(stdin);
scanf("%d",&stud[i].num);
fflush(stdin);
printf("请输入密码:\n");
fflush(stdin);
scanf("%s",stud[i].mima);
printf("请输入姓名:\n");
fflush(stdin);
scanf("%s",stud[i].name);
printf("请输入收入:\n");
fflush(stdin);
scanf("%d",&stud[i].income);
printf("请输入支出:\n");
fflush(stdin);
scanf("%d",&stud[i].payout);
printf("总收入:\n");
fflush(stdin);
scanf("%d",&stud[i].total);
}
for(i=1;i<SIZE;i++)
{
fprintf(fp,"%d %s %s %d %d %d\n",stud[i].num,stud[i].mima,stud[i].name,stud[i].income,stud[i].payout,stud[i].total);
}
fclose(fp);/*追加完数据必须关闭文件再读取,因为文件指针已经移动了不能直接读取,不然会死循环*/
if((fp=fopen("stu_list5.txt","r"))==NULL)
{
printf("文件打开失败!");
exit(0);
}
for(i=0;!feof(fp);i++)
{
fscanf(fp,"%d%s%s%d%d%d\n",&stud[i].num,stud[i].mima,stud[i].name,&stud[i].income,&stud[i].payout,&stud[i].total);
printf("%d %s %s %d %d %d\n",stud[i].num,stud[i].mima,stud[i].name,stud[i].income,stud[i].payout,stud[i].total);
}
system("pause");
fclose(fp);
}

void main()
{
LOOP: input();
while(1)
{
menu();
if(4==choice)
{
system("cls");
goto LOOP;
}
switch(choice)
{
case 1:system("cls");file_save(stud);break;
case 2:system("cls");file_read();break;
case 3:system("cls");file_rwrite();break;
default:break;
}
}
/*
for(i=0;i++)
{
fread(&stud[i],sizeof(struct student_type),1,fp);

}
fclose(fp);
printf("i为:%d",i);*/

}

⑧ 如何用c语言编程将命令行指定的一个文件的内容追加到另一个文件的末

我不清楚你在用什么平台,但凡是涉及跨进程的东西,都要直接或间接用到操作系统的系统调用.比如在Linux下,支持命令行的程序,可以在C程序中以system()函数或execv()函数进行调用,需包含unistd.h.如果是在用glib库,可以用dbus来实现进程间通信.windows编程我没学过,但机制应该类似.另外如果你的程序比较复杂,还要仔细考虑创建新进程后的管理.建议参考下windows下相应系统编程的书.

⑨ C#怎样用文件读写在文件的原有基础上追加一行数据

首先添加命名空间using System.IO:操作文件时,一定要记得及时关闭流,然后:

string path="D1.txt";//文件的路径,保证文件存在。

FileStream fs=new FileStream(path,FileMode.Append);

SteamWriter sw=new StreamWriter(fs);

sw.WriteLine(要追加的内容);

sw.Close();

fs.Close();

(9)对文件追加信息的编程扩展阅读:

关键字

fixed:在一个代码块执行时,在固定内存位置为一个变量指派一个指针。

foreach:用于遍历一个群集的元素。

goto:一个跳转语句,将程序执行重定向到一个标签语句。

ref:标识一个参数值可能会受影响的参数。

sealed:防止类型被派生,防止方法和property被覆载。

sizeof:一个操作符,以byte为单位返回一个值类型的长度。

throw:抛出一个异常。

⑩ C++中如何向文件中追加文本内容

1、首先,定义两个字符串变量str1和str2。

阅读全文

与对文件追加信息的编程相关的资料

热点内容
app反编译不完整 浏览:786
电脑上的文件夹怎么调整 浏览:5
服务器无响应是什么原因呀 浏览:984
wd文档里的app怎么制作 浏览:513
电脑里的文件夹没有了一般能恢复吗 浏览:418
哪里有配加密钥匙的 浏览:210
服务器开不了机怎么把数据弄出来 浏览:958
gif动态图片怎么压缩 浏览:521
黑猴子棒球压缩文件解压密码 浏览:631
如何让app适应不同的手机屏幕大小 浏览:10
苹果手机如何给安卓手机分享软件 浏览:761
苹果电脑怎么运行腾讯云服务器 浏览:59
明日之后沙石堡命令助手 浏览:261
蛋糕店用什么样的app 浏览:877
长安银行信用卡app怎么取现 浏览:635
dos命令cmd命令的 浏览:226
阿里云存档视频文件的服务器 浏览:194
ftp修改文件权限命令 浏览:491
周易八卦梅花算法 浏览:676
java组织机构 浏览:953