Linux 编写cpp文件一系列报错如 error:'--rgid' was not declared in this scope

qq761130493 2018-12-31 10:11:58
代码如下:
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<string.h>
#include<dirent.h>
#include<fcntl.h>
#include<ftw.h>
using namespace std;

/*
Displays the path name of the current directory.
*/
void pwd() {
char path[100];
getcwd(path,100);//Get path
cout<<"current directory: "<<path<<endl;
}

/*
List all directories and files in the specified directory name.
*/
bool list(string dir) {
DIR* d = opendir(dir.c_str());
if(d==NULL) {
return false;
} else {
struct dirent *dirent;
while(dirent=readdir(d)) {
cout<<endl;
cout<<" "<<dirent->d_name<<" "<<dirent->d_type<<" "<<dirent->d_reclen<<endl;
cout<<endl;
}
closedir(d);
return true;
}
}

/*
Change Directory.
*/
bool changedir(string path) {
if(chdir(path.c_str())==0) {
return true;
} else {
return false;
}
}

/*
New directory.
*/
bool makedir(string dir) {
if(mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)==0) {
return true;
} else {
return false;
}
}

/*
Delete directory.
*/
bool deldir(string dir) {
if(rmdir(dir.c_str())==0) {
return true;
} else {
return false;
}
}

/*
Rename a file or directory.
*/
bool rename(string lastname,string newname) {
if(rename(lastname.c_str(),newname.c_str())==0) {
return true;
} else {
return false;
}
}

/*
Copy an existing file.
*/
bool copy(string existname,string newname) {
int fo1,fo2;
char buf[1024];
fo1=open(existname.c_str(),O_RDONLY);
if(fo1==-1) {

return false;
} else {
fo2=open(newname.c_str(),O_RDONLY);
if(fo2!=-1) {
int i;
cout<<"Overwrite original file??"<<endl;
cout<<"----1 is yes,not 1 is no.";
cin>>i;
if(i!=1) {
newname+="(1)";
}
close(fo2);
}
fo2=open(newname.c_str(),O_WRONLY|O_CREAT,S_IRWXU);
int size = read(fo1,buf,sizeof(buf));
write(fo2,buf,size);
close(fo1);
close(fo2);
return true;
}
}

int num=0;
string file;
int fn(const char *fpath, const struct stat *st, int typeflag) {
for(int i=strlen(fpath)-1,j=file.length()-1;;i--,j--) {
if(j==-1&&fpath[i]=='/') {
cout<<" "<<fpath;
if(typeflag==FTW_F) cout<<" FILE"<<endl;
else if(typeflag==FTW_D) cout<<" DIRECTORY"<<endl;
num++;
break;
}
if(fpath[i]=='/') break;
if(j==-1) break;
if(fpath[i]!=file[j]) {
break;
}
}
return 0;
}

/*
Find the specified file in the specified directory and its subdirectories,
And output the absolute path to the file.
*/
bool find(string dir,string filename) {
file=filename;
ftw(dir.c_str(),fn,500);
if(num==0)
return false;
else
return true;
}

/*
Displays menu
*/
void menu() {
system("clear");
cout<<"*********************************************"<<endl;
cout<<" 1.List all directories and files in the specified directory name"<<endl;
cout<<" 2.Change Directory"<<endl;
cout<<" 3.New directory"<<endl;
cout<<" 4.Delete directory"<<endl;
cout<<" 5.Exit command interpreter."<<endl;
cout<<" 6.Rename a file or directory"<<endl;
cout<<" 7.Copy an existing file"<<endl;
cout<<" 8.Find the specified file in the specified directory and its subdirectories,And output the absolute path to the file"<<endl;
cout<<"*********************************************"<<endl;
pwd();
cout<<endl;
}

int main() {
menu();
string s;
while(1) {
cout<<"Please enter menu number:";
cin>>s;
if(s=="1") {//List all directories and files in the specified directory name.
menu();
cout<<"Please enter a specified directory name:";
string dir;
cin>>dir;
if(!list(dir)) {
cout<<"Failed to open or do not exist in this directory!!"<<endl;
}
} else if(s=="2") {//Change Directory
menu();
cout<<"Please enter a specified directory name or path:";
string path;
cin>>path;
if(!changedir(path)) {
cout<<"--->Failed to open or do not exist in this directory!!"<<endl;
} else {
cout<<"--->The current directory has been changed to "<<path<<endl;
}
} else if(s=="3") {//New directory
menu();
cout<<"Please enter a new directory name:";
string dir;
cin>>dir;
if(!makedir(dir)) {
cout<<"New directory failed!!"<<endl;
} else {
cout<<"--->Directory created successfully"<<endl;
}
} else if(s=="4") {//Delete directory
menu();
cout<<"Please enter a existing directory name:";
string dir;
cin>>dir;
if(!deldir(dir)) {
cout<<"Directory name does not exist or delete failed!!"<<endl;
} else {
cout<<"--->Directory deleted successfully"<<endl;
}
} else if(s=="5") {//Exit command interpreter
menu();
cout<<"Sign out!"<<endl;
return 0;
} else if(s=="6") {//Rename a file or directory
menu();
string lastname,newname;
cout<<"Please enter an old directory name or file name:";
cin>>lastname;
cout<<"Please enter an new directory name or file name:";
cin>>newname;
if(!rename(lastname,newname)) {
cout<<"Rename failed!!"<<endl;
} else {
cout<<"--->Rename success"<<endl;
}
} else if(s=="7") {//Copy an existing file
menu();
string existname,newname;
cout<<"Please enter an old directory name or file name:";
cin>>existname;
cout<<"Please enter an new directory name or file name:";
cin>>newname;
if(!copy(existname,newname)) {
cout<<"Copy failed!!"<<endl;
} else {
cout<<"--->Copy success"<<endl;
}
} else if(s=="8") {//Find the specified file in the specified directory and its subdirectories,And output the absolute path to the file
menu();
string dir,filename;
cout<<"Please enter an directory name:";
cin>>dir;
cout<<"Please enter an directory name or file name:";
cin>>filename;
if(!find(dir,filename)) {
cout<<"Find failure!!"<<endl;
}
} else {//error!!!!!!!!!!!!!!!
menu();
cout<<"Input error please re-enter!"<<endl;
}
}
return 0;
}
...全文
514 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
haisan000 2019-01-01
  • 打赏
  • 举报
回复
编译可以通过,没问题

g++ test.cpp -o test

qq761130493 2019-01-01
  • 打赏
  • 举报
回复
引用 3 楼 haisan000 的回复:
我的系统是centos7.
这些函数也是很普通的函数
怎么编译的?
用g++看看

试过了,不行,这个是我为了做课程设计直接从别人那里copy然后粘贴到Linux上编译的,会不会是复制黏贴的问题?
haisan000 2019-01-01
  • 打赏
  • 举报
回复
我的系统是centos7.
这些函数也是很普通的函数
怎么编译的?
用g++看看
qq761130493 2019-01-01
  • 打赏
  • 举报
回复
引用 1 楼 haisan000 的回复:
编译可以通过,没问题

g++ test.cpp -o test



可我真的编译不了啊,是Linux版本的问题吗?
VB电子相册 电子相册 1、数据库连接 Public conn As ADODB.Connection Public Sub conDB() Set conn = New ADODB.Connection conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _ & App.Path & "\data\pic.mdb" conn.Open End Sub 2、登录模块 Dim loginTimes As Integer Private rsmc As ADODB.Recordset Private rs As ADODB.Recordset Public userName As String Private Sub cmdExit_Click() Unload Me End Sub Private Sub cmdOK_Click() Call login End Sub Private Sub Form_Activate() Call conDB Set rsmc = New ADODB.Recordset rsmc.CursorLocation = adUseClient rsmc.Open "用户信息表", conn, 0, 1 'need to learn cbUserName.Clear While Not rsmc.EOF cbUserName.AddItem rsmc.Fields("用户名") rsmc.MoveNext Wend cbUserName.SetFocus tbPwd.Text = "" cbUserName.Refresh End Sub Private Sub Form_Unload(Cancel As Integer) conn.Close Set rs = Nothing End Sub Sub login() Dim strSql As String userName = "" If Trim(cbUserName.Text) = "" Then MsgBox "用户名不用为空,请选择用户名!", vbOKOnly + vbExclamation, "警告" cbUserName.SetFocus Else strSql = "select * from 用户信息表 where 用户名='" & Trim(cbUserName.Text) & "'" Set rs = New ADODB.Recordset rs.Open strSql, conn, 2, 2 If Trim(rs.Fields("密码")) = Trim(tbPwd.Text) Then rs.Close Me.Hide userName = Trim(cbUserName.Text) 'Load frmMain frmMain.Show Exit Sub Else MsgBox "密码不对,请重新输入!", vbOKOnly + vbExclamation, "警告" tbPwd.Text = "" tbPwd.SetFocus End If loginTimes = loginTimes + 1 If loginTimes = 3 Then MsgBox "密码错误已有3次,你不能进入系统!", vbOKOnly + vbQuestion, "提示" Unload Me End If End If End Sub 3、主模块 Private rs As ADODB.Recordset Dim stuNum As Integer Private Sub Form_Activate() Call conDB End Sub Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) conn.Close Set conn = Nothing End Sub Private Sub mnuAddPic_Click() frmAddPic.Show End Sub Private Sub mnuDeletePic_Click() frmDeletePic.Show End Sub Private Sub mnuShowpic_Click() frmShow.Show End Sub Private Sub mnuExit_Click() Unload Me End End Sub Private Sub mnuSMPic_Click() frmSMPic.Show End Sub Private Sub mnuUser_Click() Dim frm1 As New frmUser frm1.Show End Sub 4、显示图片模块 Dim str As String Dim rs As ADODB.Recordset Dim rsNum As Integer Dim nextNum As Integer Private Sub cbPic_Click() str = App.Path + "\" Set rs = New ADODB.Recordset Dim strConn As String strConn = "select * from pic where name='" + Trim(cbPic.Text) + "'" rs.Open strConn, conn, 0, 1 str = str + rs.Fields("address").Value 'MsgBox str Image1.Picture = LoadPicture(str) rs.Close End Sub Private Sub CmdNext_Click() nextNum = nextNum + 1 'MsgBox nextNum If nextNum > rsNum - 1 Then nextNum = 0 'MsgBox nextNum End If Dim temp As Integer temp = nextNum Set rs = New ADODB.Recordset rs.Open "pic", conn, 0, 1 ' rs.MoveFirst ' While Not rs.EOF And temp > 0 ' 'rs.MoveNext ' 'temp = temp - 1' ' Wend rs.Move (temp) str = App.Path + "\" str = str + rs.Fields("address").Value cbPic.Text = rs.Fields("name").Value Image1.Picture = LoadPicture(str) rs.Close End Sub Private Sub Form_Load() Call conDB str = App.Path + "\" nextNum = 0 Set rs = New ADODB.Recordset rs.Open "pic", conn, 0, 1 str = str + rs.Fields("address").Value Image1.Picture = LoadPicture(str) cbPic.Clear rsNum = 0 'MsgBox rsNum rs.MoveFirst While Not rs.EOF cbPic.AddItem rs.Fields("name") rsNum = rsNum + 1 rs.MoveNext Wend cbPic.Text = "tu1" rs.Close End Sub

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧