1.用write, read, open等系統調用編寫分別實現如下功能的程序(要求進行必要的出錯檢查版):
(1)創建一個權文件testfile.txt,文件內容從鍵盤輸入;
(2)將testfile.txt的內容顯示在屏幕上,並將testfile.txt的內容復制到一個新的文件file2.txt中。
實驗代碼:
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main()
2. Linux C/C++文本文件操作
你用gcc編譯器么。
3. Linux下C編程讀取文件中每行的數據
實話是,使用C如此操作是比較復雜的,步驟如下:
1、自我實現
首先需要將所有的文件讀專取到屬內存中,之後進行適當的處理,可以定義一個函數,函數的形參是四個三維數組,或者一個4*3的二維數組,之後的操作相當於矩陣的轉矩3 * 4。
2、藉助三方包
上面說了這其實就是一個矩陣的轉矩,那麼可以使用第三方的實現矩陣相關運算的庫文件。
4. linux c怎麼實現從文件的最後一行一行向前讀文件
下面的例子使用mmap讀最後20行(假設最後20行不會超過位元組)
/*-
* Copyright (C), 1988-2014, mymtom
*
* vi:set ts=4 sw=4:
*/
#ifndef lint
static const char rcsid[] = "$Id$";
#endif /* not lint */
/**
* @file last20.c
* @brief
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
char *memchrr(const void *v1, const char *v2, int c)
{
char *s1, *s2;
char *p;
s1 = (char *)v1;
s2 = (char *)v2;
for (p = s2; p >= s1; --p) {
if (*p == c)
return p;
}
return NULL;
}
#define READSIZE 1024
int main(int argc, char *argv[])
{
int ret;
FILE *fp;
char *addr;
size_t len;
int prot;
int flags;
int fd;
off_t off;
off_t rem;
long pagesize;
struct stat buf;
pagesize = sysconf(_SC_PAGESIZE);
fp = fopen("last20.c", "rb");
fd = fileno(fp);
ret = fstat(fd, &buf);
if (buf.st_size <= READSIZE || buf.st_size <= pagesize) {
off = 0;
len = buf.st_size;
} else {
off = buf.st_size - READSIZE;
rem = off % pagesize;
off = off - rem;
len = READSIZE + rem;
}
/*
printf("size=%d READSIZE=%d off=%d len=%d\n",
(int)buf.st_size, (int)READSIZE, (int)off, (int)len);
*/
prot = PROT_READ;
flags = MAP_PRIVATE;
addr = mmap(NULL, len, prot, flags, fd, off);
fclose(fp);
{
int i, n;
char *head, *tail;
size_t size;
char line[1024];
tail = addr + len - 1;
n = 20;
for (i = 0; i < n; ++i) {
head = memchrr(addr, tail - 1, '\n');
if (head == NULL) {
size = tail - addr;
memcpy(line, addr, size);
line[size] = '\0';
} else {
size = tail - head - 1;
memcpy(line, head + 1, size);
line[size] = '\0';
tail = head;
}
printf("%s\n", line);
if (head == NULL) {
break;
}
}
}
munmap(addr, len);
return 0;
}
運行結果為:
./last20 | tac | cat -n
line[size] = '\0';
} else {
size = tail - head - 1;
memcpy(line, head + 1, size);
line[size] = '\0';
tail = head;
}
printf("%s\n", line);
if (head == NULL) {
break;
}
}
}
munmap(addr, len);
return 0;
}
5. Linux系統中如何操作文件
Linux里主要有兩自種文件操作方式:系統調用、ANSI C文件操作.
(1) Linux系統調用
一個非負整數,索引值,通常使用宏來表示。代表指向內核中每個進程打開文件的記錄表。
即底層操作。文件底層I/O操作的系統調用主要有open、close、read、write、lseek、ulink函數來直接操作文件。
其實ZLG那有很多的,你可以去看一下的。
6. C語言文件讀取問題 linux
剛按你的需求寫了個小程序,看看這個能滿足你的要求不,
遍歷一個目錄,查找不是隱藏的文件,然後輸出出來,
至於你要干什麼嘛,名都給你遍歷出來,估計你自己就會做了吧.
這個遍歷不是深度的,只遍歷你輸入的目錄的當前目錄.
編譯的話,帶gcc的環境,把這個隨便起個名
gcc
xxx.c
就行了..
運行時要加個參數
比如,生成的可執行程序是a.out
./a.out
/root/--->加的參數就是你要遍歷的路徑.
#include
<sys/types.h>
#include
<sys/stat.h>
#include
<time.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<dirent.h>
int
main(int
argc,char
**argv)
{
struct
stat
s;
struct
stat
ss;
DIR
*dir
=
NULL;
struct
dirent
*dt
=
NULL;
if
(argc
!=
2)
{
printf("./xxx
dirpath
to
run
this
proc\n");
return
1;
}
if(lstat(argv[1],&s)
<
0)
{
printf("lstat
error\n");
return
1;
}
if(S_ISDIR(s.st_mode))
{//確定path是dir
dir
=
opendir(argv[1]);
if
(dir
==
NULL)
{
printf("open
dir
error\n");
return
1;
}
if
(chdir(argv[1])
<
0)
{//進入目錄
printf("chdir
error\n");
return
1;
}
while((dt
=
readdir(dir))
!=
NULL)
{
if(dt->d_name[0]
==
'.')
{//隱藏文件或./..目錄不進行查找
continue;
}
if(lstat(dt->d_name,
&ss)
<
0)
{
continue;
}
if
(S_ISREG(ss.st_mode))
{//列印普通文件
printf("file:%s\n",dt->d_name);
}
}
}
return
0;
}
7. linux下c語言 讀取文件內容
沒測試過,不過問題應該是fgetc這里
fgetc獲取到第一個字元,比如第一行的'#'號,然後fgets獲取到後面的字元,列印版當然就沒有權第一個字元了,解決方式要麼只用fgets,要麼把fgetc獲取的字元也列印出來
8. 關於linux C的文件讀寫
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1024
void lower1(char *p)
{
int i,len;
len = strlen(p);
for(i=0;i<len;i++)
if(p[i] >= 'A' && p[i] <= 'Z')
p[i] += 32;
}
int main(void)
{
FILE *fp,*fpw;
char *p;
char buf[MAX],buf1[MAX]="GAME OVER";
int n,m;
fp = fopen("txt","rw");
if(fp == NULL)
{
perror("Fail to open");
exit(1);
}
while((n = fread(buf,sizeof(char),MAX-1,fp)) > 0)
{
buf[n]='\0';
lower1(buf);
printf("%s",buf);
printf("%d",n);
}
rewind(fp);
while((n = fread(buf,sizeof(char),MAX-1,fp)) > 0) //你這里什麼意思?你這里有問題
{
fputs(buf,fp);
}
if(n < 0){
perror("Fail to read");
exit(1);
}
fclose(fp);
return 0;
}
順便,看樣子你也知道,讀寫無法同時進行的,所以,你讀萬,一定要rewind一下
9. linux下如何用C程序讀寫本地文件
是一樣來的。如果是同目自錄則直接寫文件名,如果是不同的目錄,可以寫明路徑。
如:
讀同目錄文件local.txt
fopen("local.txt","r");
讀不同目錄文件 /home/yourname/otherdir/other.txt
fopen("/home/yourname/otherdir/other.txt","r");
你可以使用pwd命令來獲得文件路徑
10. c語言如何讀寫 linux文本文件
Linux下C語言的文件(fputc,fgetc,fwrite,fread對文件讀寫操作)
//
fputc 向文件寫入字元
#include <stdio.h>
#include <stdlib.h>
main()
{
FILE *fp;
char ch;
if((fp=fopen("test.txt","w"))==NULL)
{
printf("不能打開文件 ");
exit(0);
}
while ((ch=getchar())!=' ')
fputc( ch, fp );
fclose(fp);
}
-------------
小提示:
fp=fopen("test.txt","w") ,把"w"改為 "a" 可以創建文件並且追加寫入內容
exit(0); 需要包含 stdlib.h 頭文件,才能使用
//
fgetc 讀取字元
#include <stdio.h>
#include <stdlib.h>
main( int argc, char *argv[] )
{
char ch;
FILE *fp;
int i;
if((fp=fopen(argv[1],"r"))==NULL)
{
printf("不能打開文件 ");
exit(0);
}
while ((ch=fgetc(fp))!=EOF)
putchar(ch);
fclose(fp);
}
文件結尾,通過判斷 EOF
//
fwrite 的使用
使數組或結構體等類型可以進行一次性讀寫
#include <stdio.h>
#include <stdlib.h>
main()
{
FILE *fp1;
int i;
struct student{
char name[10];
int age;
float score[2];
char addr[15];
}stu;
if((fp1=fopen("test.txt","wb"))==NULL)
{
printf("不能打開文件");
exit(0);
}
printf("請輸入信息,姓名 年齡 分數1 分數2 地址: ");
for( i=0;i<2;i++)
{
scanf("%s %d %f %f %s",stu.name,&stu.age,&stu.score[0],&stu.score[1], stu.addr);
fwrite(&stu,sizeof(stu),1,fp1);
}
fclose(fp1);
}
//
fread 的使用
#include <stdio.h>
#include <stdlib.h>
main()
{
FILE *fp1;
int i;
struct student{
char name[10];
int age;
float score[2];
char addr[15];
}stu;
if((fp1=fopen("test.txt","rb"))==NULL)
{
printf("不能打開文件");
exit(0);
}
printf("讀取文件的內容如下: ");
for (i=0;i<2;i++)
{
fread(&stu,sizeof(stu),1,fp1);
printf("%s %d %7.2f %7.2f %s ",stu.name,stu.age,stu.score[0],stu.score[1],stu.addr);
}
fclose(fp1);
}
//
fprintf , fscanf, putw , getw , rewind , fseek 函數
這些函數的話我就不演示了 ,
這些函數基本都一對來使用,例如 fputc 和 fgetc 一起來用.