如何用C语言建立一个类似Linux的文件系统,文件 c语言

文章 2年前 (2021) admin
0

Q1:用c语言编写一个linux程序:通过find一个文件名,系统给一个路径,然后用c语言展开这个路径

# include # include # include in(intargc,char * argv[]){ char file _ name[64]={ 0 };char file _ path[128]={ 0 };chardir _ path[64]={ 0 };char cmd[256]={ 0 };int re=0;文件* pf=空;char * p=NULLprintf("请输入文件名: \ n ");scanf("% s ",file _ name);snprintf(cmd,256," find-name%stmp.txt ",file _ name);//如果文件不在程序运行的本地目录(或者子目录)下,则需要改成find/-namexxxx但耗时会很长系统(cmd);//文件中取绝对路径pf=fopen("tmp.txt "," r ");if(pf){re=fscanf(pf," %s ",file _ path);//只取第一行也就是默认不存在同名文件if(-1==re){ printf(" err0 \ n ");返回0;} else { flcose(pf);pf=空;} } else { printf(" err1 \ n ");返回0;} if(0==strlen(file _ path)){ printf(" err 2 \ n ");返回0;}//获取目录名p=strtr(file _ path,file _ name);if(p){ * p=" \ 0 ";strcpy(dir_path,file _ path);//也可以不丙酸纤维素直接用file_path}//通过c语言展开限位开关(限位开关)这个路径c语言变量其实我没有理解这句话.先这么做吧snprintf(cmd,256," ls%s ",dir _ path);系统(cmd);返回0;}如果哪里有异常就加打印函数打印出来各个变量

Q2:如何用C语言写一个Linux shell

#include #include int main() { //exec 系列函数出错时会返回 -1,平常返回 0,所以可以 //据此来打印错误信息 //第一个 ls 是命令的名称,execlp 函数会自动在 $PATH //中寻找这个命令。 //后面一个 ls 是要在 shell 中输入的第一个参数 //(也就是命令名称本身) //使用 NULL 作为参数结尾标记是 exec 系列函数的要求。 if (execlp("ls", "ls", "-l", NULL) == -1) perror("Error Executing Command.\n"); return 0;}

Q3:用C语言编写程序完成以下的Unix/Linux系统的系统调用

//这个是我学Unix C++时对知识点总结程序,应该对你有些用吧。/*istream ostream | \ / |ifstream iostream ofstream | fstream*/#include #include using namespace std;int main(){ char ch[6]; cout << "Input a line: "; ch[5] = cin.peek(); cout << "Peek first char: " << ch[5] << endl; cin.get(ch[4]); cout << "Read first char: " << ch[4] << endl; cin.putback(ch[4]); cin.getline(ch, 5); if(!cin) { cout << "Input error!!!" << endl; cin.clear(); cin.ignore(100, "\n"); } else cout << ch << endl; cout << left << hex << showbase << uppercase << 1234 << endl; cout << scientific << uppercase << left << 0.00123456 << endl; cout.fill("*"); cout.width(8); cout.unsetf(ios::dec | ios::oct); cout.setf(ios::hex | ios::showbase | ios::uppercase | ios::left); cout << 1234 << endl; cout.setf(ios::dec); ofstream fout("data.txt"); //使用fstream fio("data.txt", ios::in | ios::out);既可读又可写。 if(!fout.fail()) { fout << "3.14159\t64I love C++" << endl; fout.put("M"); fout.put("\n"); double d = 0.0123456789; fout.precision(4); fout.setf(ios::fixed);// fout.setf(ios::scientific | ios::uppercase); fout << d << endl; fout.seekp(3, ios::cur); //读使用seekg(),与seekp()类似。 fout << "*" << flush; fout.seekp(3, ios::cur); fout << endl; fout.seekp(2, ios::cur); fout << "@" << " " << "*" << " " << "@" << endl; fout.seekp(2, ios::cur); streampos j = fout.tellp(); while((fout.tellp()-j) < 5) fout << "*" << flush; cout << "文件写入成功!" << endl; } else cout << "文件打开失败!" <> d; cout << d << endl; fin.get(ch[0]); for(int i=0; i<2; i++) { fin.get(ch[i]); } cout << ch << endl; while(!fin.eof()) { fin.getline(ch, 20, "\n"); cout << ch << endl; } } else cout << "文件打开失败!" << endl; fin.close(); ofstream fo; fo.open("bin.bin", ios::binary|ios::app); if(!fo.fail()) { int a[10]; for(int i=0; i<10; i++) a[i] = i; fo.write((char *)a, sizeof(a)); } else cout << "文件打开失败!" << endl; fo.close(); ifstream fi; fi.open("bin.bin", ios::binary|ios::in); if(!fi.fail()) { int a[10]; fi.read((char *)a, sizeof(a)); fi.close(); for(int i=0; i<10; i++) cout << a[i]; cout << endl; } else cout << "文件打开失败!" << endl; fi.close(); return 0;}

Q4:学习c语言 需要一个linux环境 求在windows中如何弄一个linux ?

要么虚拟机,要么双系统,那种直接安装在windows中创建20G作为硬盘的也是属于双系统,只是没有修改硬盘分区。虚拟机建议安装CentOS或Debian这些界面简洁的,运行能快一些。双系统就可以考虑ubuntu、deepin这些。不用虚拟机也不要双系统,只有一个叫cygwin的东东,可以添加linux的各种软件,但主要还是些命令行程序。里面可以添加各种linux命令,写C程序的话,可以添加gcc、gdb、make这些,然后用文本编辑器写代码,通过命令进行编译及调试。

Q5:在Linux下用C语言做个程序 创建一个txt文件 执行程序写一段话 写进txt

示例教程:1。编写代码以使用命令创建main.c文件vimmain.cmain.c代码:# include # include # include # include # include # include # definema x50 int main(){ int FD;charbuf[MAX];fd=open("1.txt ",O _ WRONLY | O _ CREAT | O _ APPEND);If(fd==-1){printf("文件创建失败");返回-1;}printf("请输入要写的句子:");fgets(buf、MAX、stdin);写(fd、buf、MAX);Printf("写入成功\ n ");返回0;}2.编译源程序gcc main . c-domain。因此,主源程序就会以这种方式出现。3.运行源程序,查看文件是否已创建,写入是否已成功完成。

Q6:Linux编程,怎样用C语言创建、读、写文件呀?希望各位赐教!谢谢

楼上正解,提供一小例子如下,另外,linux 下C读写文件函数有两种,一个是系统的,还有一个是C函数库的记得,好像是系统的文件句柄(file id)比较简单,然后函数库的那个提供了一个结构体,具体的记不请了....好长时间没写...#include #include int main(int argc, char **argv){ int fid; char buff[4096]; fid = open(argv[1],O_WRONLY); if (fid==-1) { perror(argv[1]); exit(1); } while (fgets(buff,4096,stdin)!=NULL) { write(fid,buff,strlen(buff)); } close(fid);}

版权声明:admin 发表于 2021年11月9日 上午8:57。
转载请注明:如何用C语言建立一个类似Linux的文件系统,文件 c语言 | 热豆腐网址之家

相关文章