2013-02-23 48 views
0

我想将文本文件的内容读入到二维字符串数组中,但无论我尝试或搜索了什么,我都找不到解决方案。该代码应该加载一个文本文件,通过查找水平制表符并显示输出,将其分隔为多个元素。当我按照原样运行代码时,我收到一条从在线搜索中发现的错误,这意味着我正试图操纵我不该做的内存。我并没有要求编写代码,只是朝着正确的方向发展。先谢谢你。使用getline从文件读取到2d字符串数组

这是我所得到的,当我运行程序:

0x23fd5c 

Process returned -1073741819 (0xC0000005) execution time : 2.344 s 
Press any key to continue. 

编辑::我已经纠正代码,以便它现在的功能,因为它应该,但它不是存储每一行​​的最后一个条目正确的文本文件。我可以以某种方式显示最后一个条目的数字100,但是当我尝试传递该位置或仅显示playList [0] [5]时,它表示它等于下一行的第一个条目。任何帮助将是惊人的我发布了下面的当前代码。

这里是我的代码:

#include <iostream> 
#include <string> 
#include <iomanip> 
#include <cstdlib> 

using namespace std; 

void readTextFile(int &Count, string playList[50][5]); 
void userAddition(int &Count, string playList[50][5]); 




int main() 
{ 
string decision; 
string playList[50][5]; 
int Count = 0; 
readTextFile(Count, playList); 

cout << "If you would like to add to the list, Please enter 'Y'. If you would like to exit  
please enter 'N'. ----> "; 
getline(cin, decision); 
if (decision=="y" || decision=="Y") 
    userAddition(Count, playList); 
else 
{ 
    return(0); 
} 

return 0; 
} // End of Main FN. 






void readTextFile(int &Count, string playList[50][5]) 
{ 

string inputfield; 

ifstream infile("c:\\cTunes.txt", ifstream::in); 
    if (infile.is_open()) 
    { 
     // File is read. 
    } // end if 
    else 
    { 
     cout << "Error Opening file" << endl; 
     return; //Program Closes. 
    } // end else 

    cout << setw(30)<<left<< "TITLE"<< setw(10) <<left<<"LENGTH"<< 
     // Outputs a title to   each column that is displayed. 
    setw(40)<< left<<"ARTIST"<< setw(40) << left<<"ALBUM"<< 
    setw(15) << left <<"GENRE" << setw(5) << left << "RATING" << endl; 

getline(infile, inputfield, '\t'); // read until tab 
while(! infile.eof()) // loop until file is no longer valid. 
{ 

     playList[Count][0] = inputfield; 
     getline(infile, inputfield, '\t');   // read until tab. 

     playList[Count][1] = inputfield; 
     getline(infile, inputfield, '\t');    // read until tab. 

     playList[Count][2] = inputfield; 
     getline(infile, inputfield, '\t');   // read until tab. 

     playList[Count][3] = inputfield; 
     getline(infile, inputfield, '\t');   // read until tab. 

     playList[Count][4] = inputfield; 
     getline(infile, inputfield);    // read until end of line. 
     playList[Count][5] = inputfield; 

    cout << setw(30)<<left<< playList[Count][0] << setw(10) <<left<<playList[Count][1] <<   
    // Output the line number equal to count. 
    setw(40)<< left<<playList[Count][2] << setw(40) << left<< playList[Count][3] << 
    setw(15) << left << playList[Count][4] << setw(5) << left << playList[Count][5] <<      
    endl; 

    /*cout <<"Title: " << setw(25)<<left<< playList[Count][0]<<endl; 
    cout <<"Length: " << setw(5) <<left<<playList[Count][1] << endl; 
    cout <<"Artist: " << setw(50)<< left<<playList[Count][2] << endl; 
    cout <<"Album: " << setw(40) << left<< playList[Count][3] << endl; 
    cout <<"Genre: " << setw(15) << left << playList[Count][4] << endl; 
    cout <<"Rating: " << setw(5) << left << playList[Count][5] << endl<<endl;*/ 

    Count++;   // Increment counter by 1 
    getline(infile, inputfield, '\t'); // read next line until tab. 

    } // end while 
infile.close(); // close the file being read from. 
cout<<endl<<endl<<playList[0][5]<<endl; 
} // End of readTextFile 

相信看完,直到行结束的时候,但我不知所措我真正的函数getline引起的问题。

+1

'string playList [19] [5];'为什么?为什么不使用'vector >'? – us2012 2013-02-23 23:31:10

+0

如果您附加了示例输入+预期输出,您将得到更适合您的特定情况的答案。 – LihO 2013-02-24 00:05:24

+0

还要注意,当打开文件时出现错误:'else {cout <<“Error Opening file”<< endl; }'你应该使用'return'来防止你的函数的其他部分被执行。 – LihO 2013-02-24 00:07:26

回答

0

首先,请注意以下行:

cout << playList << endl; 

正在打印的阵列,而不是内容的地址。您将需要 循环打印内容。

其次,在下面的代码:

if (infile.is_open()) 
{ 
    // ok, read in the file 
} // end if 
else 
{ 
cout << "Error Opening file" << endl; 
//a return statement is missing 
} 

你需要一个return语句,以避免从一个封闭的流读取(这可能会导致系统崩溃)

最后,你的函数不返回任何东西,所以它应该被宣布为无效 或返回一些值(根据上下文没有意义)。

希望有所帮助,祝你好运!

+0

谢谢你的帮助。打印地址是否会给我上面发布的错误?当我运行代码时,windows提示我关闭程序,而不是编译器,这导致我相信我正在访问我不应该的东西。我将函数切换为void,并在错误检查中添加了返回值。 – 2013-02-24 00:34:23

+0

很难说,因为当我使用minGW编译器编译 时,即使在修复之前,您的代码对我来说工作正常。您是否在使用 visual studio? – 2013-02-24 00:47:21

+0

我已经尝试过Visual Studio和CodeBlocks :-(我一直试图让它工作6小时,我唯一的想法是我的电脑不允许编译器访问该文件 – 2013-02-24 00:57:25

0

您正在使用std::string用于存储字符串,std::ifstream从文件读取,std::getline阅读的话...你应该使用,而不是阵列std::vector S:

typedef std::vector<std::string> Line; 
std::vector<Line> playList; 

这将使事情变得更容易了您。 我还建议你改变你从文件中读取的方式:

while(getline(...)) 
{ 
    ... 
} 

但因为你不准使用std::vector,你的函数看起来是这样的:

// reads the content of input file and stores it into playList: 
void readTextFile(std::ifstream& infile, std::string playList[][5]) 
{ 
    std::string line; 
    for (int lineIndex = 0; getline(infile, line); ++lineIndex) 
    { 
     // skip empty lines: 
     if (line.empty()) continue; 

     std::string word; 
     std::istringstream lineStream(line); 
     for (int wordIndex = 0; getline(lineStream, word, '\t'); ++wordIndex) 
     { 
      // skip empty words: 
      if (line.empty()) continue; 

      playList[lineIndex][wordIndex] = word; 
     } 
    } 
} 

可以这样使用:

std::string playList[19][5]; 

std::ifstream infile("c:\\Test.txt"); 
if (infile.is_open()) 
{ 
    readTextFile(infile, playList); 
    infile.close(); 
} 
else 
    std::cout << "Error Opening file" << std::endl; 

还要注意的是cout << playList << endl;输出第一个单词的地址。要打印所有单词,您必须编写一个循环。

+0

感谢您的快速响应!我目前在一个非常基础的C++编程课,我们还没有包含向量,所以我不认为这是可以接受的,因为我不幸地使用它们。 – 2013-02-24 00:01:53

+0

@Davidtftyfy:现在查看我的答案:)请注意,给定的解决方案依赖于文件中每行内不超过5个单词的事实。 – LihO 2013-02-24 01:08:52

+0

好吧,我会试试看。感谢您投入此时间。我很惊讶这个网站有帮助的人,这是我的第一篇文章,所以我不知道会发生什么。 – 2013-02-24 01:20:40

0

readTextFile函数不返回任何东西。你需要返回一个字符串。如果你得到错误what(): basic_string::_S_construct null not valid那么这是你的问题。您可以通过使用-Wall编译器选项来避免类似的问题。

相关问题