这是我的问题。我正在运行一个我在网上找到的程序,但它无法编译。当我实际使用字符串时,为什么会出现错误:"array initializer must be an initializer list or string literal"
?我知道这个代码适用于其他运行windows的人,但不适用于我在m1 Mac上。
我正在尝试使用初始化string
阵列
string f[3] = " ";
#include <iostream>
#include <cstdio>
#include <string.h>
#include <unistd.h>
#include <cstdlib>
using namespace std;
int main()
{
bool cont = true;
char buffer[100] = {};
while (cont)
{
cout << "nnEnter a command: ";
cin.get (buffer, 90);
//REMOVE
if (strstr (buffer,"remove") != NULL)
{
char* t = strchr (buffer,' ');
if(t != NULL)
{
if(remove( t+1 ) != 0)
cout << "nnError deleting the file.";
else
cout << "nnThe file has successfully been deleted.";
}
else
cout << "nnInvalid command. Filename not entered.";
}
//EXIT
else if (strstr (buffer,"exit") != NULL)
cont = false;
//RENAME
else if (strstr (buffer,"rename") != NULL)
{
char* t = strchr (buffer,' ');
if (t != NULL)
{
char* oldName = t + 1;
char* newName = strchr (oldName, ' ');
if (newName != NULL)
{
char temp[30] = {};
int i = 0;
for(char* start = oldName; start != newName; start++)
{
temp[i] = *start;
i++;
}
newName++;
if(rename( temp , newName ) != 0)
cout << "nError renaming the file.";
else
cout << "nThe file has successfully been renamed.";
}
else
cout << "nnNew Name of the file not entered.";
}
else
cout << "nnInvalid command.";
}
//RMDIR
else if (strstr (buffer,"rmdir") != NULL)
{
char* t = strchr (buffer,' ');
if(t != NULL)
{
if(rmdir( t+1 ) != 0)
cout << "nnError deleting the directory.";
else
cout << "nnThe directory has successfully been removed.";
}
else
cout << "nnInvalid command. DirectoryName not entered.";
}
//ECHO
else if (strstr (buffer,"echo") != NULL)
{
char* t = strchr (buffer,'"');
if (t != NULL)
{
char* data = t + 1;
//Extracting the data
char temp[200] = {};
int i = 0;
for(; data[i] != '"'; i++)
{
temp[i] = data[i];
}
//Checking if filename is given or not
char* fileN = strchr (data + i, ' ') ;
if (fileN != NULL)
{
fileN++;
// create a FILE typed pointer
FILE *file_pointer;
// open the file for writing
file_pointer = fopen (fileN, "w");
if (file_pointer != NULL)
{
// Write to the file
fprintf (file_pointer,"%s", temp);
// Close the file
fclose (file_pointer);
cout << "nnThe file has been successfully created.";
}
else
cout << "nnCould not create the file.";
}
//If filename isn't given then simply print the data on console
else
{
cout << endl << endl << temp;
}
}
else
cout << "nnInvalid command. Data not given";
}
//UNZIP
else if (strstr (buffer,"unzip") != NULL)
{
char* t = strchr (buffer,' ');
if(t != NULL)
{
char temp[50] = "tar xvf";
if( system( strcat(temp,t) ) != 0 )
cout << "nnError unzipping the file.";
else
cout << "nnThe file has been successfully unzipped.";
}
else
cout << "nnInvalid command. FileName not entered.";
}
//ZIP
else if (strstr (buffer,"zip") != NULL)
{
char* t = strchr (buffer,' ');
if(t != NULL)
{
char temp[50] = "tar cvf";
if( system( strcat( temp,t) ) != 0 )
cout << "nnError zipping the file.";
else
cout << "nnThe file has been successfully zipped.";
}
else
cout << "nnInvalid command.";
}
else if (strstr (buffer,"out") != NULL)
{
int i = 1;
//Checking '-l'
bool lineByLine = false;
char* lpos = strstr (buffer,"-l");
if (lpos != NULL)
{
lineByLine = true;
i++;
}
string s(buffer);
string fileN = "";
string delimiter = " ";
size_t pos = 0;
while ( i > 0 )
{
pos = s.find(delimiter);
s.erase(0, pos + delimiter.length());
i--;
}
//Now extracting the file names
string f[3] = " ";
i = 0;
while ( (pos = s.find(delimiter)) != -1)
{
f[i] = s.substr(0, pos);
s.erase(0, pos + delimiter.length());
i++;
}
//if atleast one filename is present
if ( s != "out" && s != "-l" && s != "" )
{
f[i] = s;
//Opening the files and printing the contents
int c;
FILE *file;
int j = 0;
bool delay = false;
char x;
while ( j <= i)
{
char fName[50];
strcpy(fName, f[j].c_str());
//Printing the contents of the file(s)
file = fopen(fName, "r");
char line[256];
cout << "nnThe contents of the file " << fName << " are as follows : nn";
if (file)
{
if (lineByLine)
{
while (fgets(line, sizeof(line), file))
{
printf("%s", line);
//Delay loop
delay = true;
while(delay)
{
cout << "nnPress some key to print the next linenn";
getchar();
delay = false;
}
}
}
else
{
while ((c = getc(file)) != EOF)
putchar(c);
}
fclose(file);
}
else
cout << "nnCould not open the file " << fName << " .";
j++;
//Delay loop
delay = true;
while(delay)
{
cout << "nnPress some key to continuenn";
getchar();
delay = false;
}
}
}
else
cout << "nnNo filename entered.nn";
}
else
cout << "nnInvalid Command. Kindly enter a valid command.";
cin.ignore();
}
cout << "nnnExiting the CLI.nn";
return 0;
}
如果您试图创建长度为3的字符串,您应该这样做:
string f(3, ' ');
如果您试图创建一个由3个不同字符串组成的数组,那么代码的问题是类型不匹配-您试图将一个单个字符串分配给一个字符串数组。相反,你会想这样做:
string f[3] = {" ", " ", " "};
查看您的代码,您似乎想要创建一个字符串数组。然而,看起来你也可以读取任何数量的字符串(除非你的代码中有一些逻辑,你确信它总是3?(,所以我建议使用std::vector
。
std::vector<std::string> f;
您可以使用push_back()
方法向其添加字符串,如下所示:
f.push_back(s.substr(0, pos));