2014-11-05 67 views
-2

我附上了我的程序的完整源代码,可以打开.txt文件。它不会在cout << length之后执行。我试图通过使用一个数组来将.txt文件信息存储在内存中。为什么下一行不能执行C++

#include <iostream> 
#include <string.h> 
#include <fstream> 
using namespace std; 

char filename[128]; 
char file[10][250]; 
int count; 
int length; 
string line; 

int main() 
{ 
    int count = 0; 
    int length = 0; 
    cout << "Filename: "; 
    cin.clear(); 
    cin.getline(filename, sizeof(filename)); 
    string new_inputfile(filename); 
    ifstream inputfiles (new_inputfile.c_str()); 
    if(!inputfiles.is_open()) 
    { 
     cout << "File could not be opened. \n "; 
    } 
    else 
    { 
     for (int i=0; getline(inputfiles,line); i++) 
     { 
      length++; 
     } 

     cout << length; 
//  char file[length][250]; <- How can I create the array based on the length variable? 
// CODE DOES NOT EXECUTE AFTER THIS. 
     while(!inputfiles.eof() && (count<10)) 
     { 
      inputfiles.getline(file[count],250); 
      count++; 
     } 

     for(int i=0; i < count; i++) 
     { 
      cout << file[i] << endl; 
     } 

    } 
    inputfiles.close(); 
    return 0; 
} 

而且,由于file[]是char,比方说file[1]包含的字符Name=Mike,我怎么脱光=之前的一切。我只想要Mike。我知道用string,我可以用substr()的方法,但我不知道用于char

+0

为什么不直接使用'getline(cin,new_inputfile);'直接跳过中间人? – 2014-11-05 21:08:11

+0

文件不是'char',它是一个'char数组'差别很大。 – deW1 2014-11-05 21:09:56

+0

@NeilKirk你是什么意思?我在哪里放置getline(cin,new_inputfile)'? – user4167396 2014-11-05 21:10:21

回答

3

这是非常浪费的方式来计算文件中的行数。

for (int i=0; getline(inputfiles,line); i++) // i is also completely useless here 
{ 
    length++; 
} 

您正在阅读整个文件只是为了抛开一切,重新开始!此循环完成后,inputfiles.eof()将为true,并且您将永远不会输入下一个while循环,也不会输入最后的for循环(因为i == count)。执行直接跳到inputfiles.close(),然后从main返回。

,我建议你在line字符串工作,当您去:

for (; getline(inputfiles, line);) 
{ 
    // do stuff with line and ditch the global char arrays 
} 

如果你想存储供以后的线路,那么,就拯救他们:)来最容易的事情就是用一个vector

std::vector<std::string> all_them_lines; 
while (getline(file, line) all_them_lines.emplace_back(line); 

在那里,整个文件现在由线保存在all_them_lines,行。你可以像访问数组一样访问它们,如all_them_lines[0]。你也不需要事先知道行数 - 当你向他们添加东西时矢量会自动扩展。

现在解析一行并从中提取格式化的输入,看看stringstream class必须提供什么。

+0

谢谢。所以我如何保存这条线?我听说你可以不用数组。就像我的'txt'文件中的第一行是'Name = Mike',我想将'name'变量设置为'Mike'。我的第二行是'Age = 10',我想将我的'age'变量设置为10 – user4167396 2014-11-05 21:15:29

+0

@ user4167396我稍微扩展了答案。 – jrok 2014-11-05 21:20:43

+0

谢谢:)。是否有读取文件的方式,不是存储行,而是将行存储到变量中。例如,如果我的'txt'文件中的第一行是'Name = Mike',并且我想将'name'变量设置为'Mike'。我的第二行是'年龄= 10',我想将我的'age'变量设置为'10'。我打算创建一个'setName'和'setAge'方法来存储行 – user4167396 2014-11-05 21:27:39

-1

你问:

//  char file[length][250]; <- How can I create the array based on the length variable? 

声明file为:

char (*file)[250] = NULL; 

然后,

file = new char[length][250]; 

确保您的年底之前调用delete [] file功能。

你说:

// CODE DOES NOT EXECUTE AFTER THIS. 

您可以rewind the stream,并开始从它再次阅读。

inputfiles.seekg(0); 
    count = 0; 
    while(!inputfiles.eof()) 
    { 
     inputfiles.getline(file[count],250); 
     count++; 
    } 
+0

-1,使用向量或其他容器而不是新/删除 – 2014-11-05 21:37:34

+0

@MattMcNabb,我很欣赏评论。回答OP的问题对我来说更有意义,而不是建议一种不同但更好的方法。有时我确实提出了一种不同的方法。对于这个问题,我觉得回答这个问题更有意义。 – 2014-11-05 21:46:07

+0

我不确定你是什么意思的“点”。 'char file [length] [250]'是非法的,必须由一些合法代码替换。 – 2014-11-05 22:00:16