導航:首頁 > 編程語言 > 對文件追加信息的編程

對文件追加信息的編程

發布時間: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。

閱讀全文

與對文件追加信息的編程相關的資料

熱點內容
java合並鏈表 瀏覽:505
pic單片機編譯器 瀏覽:803
麗水四軸加工中心編程 瀏覽:689
國產系統怎麼解壓 瀏覽:552
戰雙程序員 瀏覽:483
him觸摸編程軟體 瀏覽:931
植物大戰僵屍存檔怎麼轉移安卓 瀏覽:852
java棧的元素 瀏覽:737
程序員與籃球事件 瀏覽:675
app反編譯不完整 瀏覽:788
電腦上的文件夾怎麼調整 瀏覽:7
伺服器無響應是什麼原因呀 瀏覽:984
wd文檔里的app怎麼製作 瀏覽:513
電腦里的文件夾沒有了一般能恢復嗎 瀏覽:418
哪裡有配加密鑰匙的 瀏覽:210
伺服器開不了機怎麼把數據弄出來 瀏覽:958
gif動態圖片怎麼壓縮 瀏覽:521
黑猴子棒球壓縮文件解壓密碼 瀏覽:631
如何讓app適應不同的手機屏幕大小 瀏覽:10
蘋果手機如何給安卓手機分享軟體 瀏覽:761