C++ 读取文件int结构时的状态\u访问\u冲突
我正在将三样东西读入一首结构歌曲:歌曲标题、艺术家、文件大小。我运行程序时出错,尽管看起来正确C++ 读取文件int结构时的状态\u访问\u冲突,c++,struct,C++,Struct,我正在将三样东西读入一首结构歌曲:歌曲标题、艺术家、文件大小。我运行程序时出错,尽管看起来正确 #include <iostream> #include <string> #include <sstream> #include <fstream> using namespace std; struct Songs { string title; string artist; int size; }; int main (
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
struct Songs
{
string title;
string artist;
int size;
};
int main ()
{
int num_songs;
Songs song[num_songs];
ifstream fin;
fin.open("songlist.txt")
while (fin.good()) {
fin >> song[num_songs].title;
fin >> song[num_songs].artist;
fin >> song[num_songs].size;
num_songs++;
}
fin.close();
cout << "welcome to the show" << endl;
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
结构歌曲
{
字符串标题;
弦乐艺术家;
整数大小;
};
int main()
{
int num_歌曲;
歌曲歌曲[数字歌曲];
流鳍;
财务公开(“songlist.txt”)
while(fin.good()){
fin>>歌曲[num_歌曲]。标题;
歌曲[num_歌曲]。艺术家;
fin>>歌曲[num_歌曲]。大小;
num_songs++;
}
fin.close();
cout你的代码太疯狂了……song数组有多大?目前你正在将其初始化为“未定义”大小。你必须初始化num_songs。如果不初始化,则意味着它“可能”使用任何值。因为你将数组初始化为num_songs大小,所以你会遇到访问冲突(我们将以任意数字70为例,尽管它可以是任何数字),然后您开始以相同的值(超过数组末尾)写入数组。这是一种访问冲突,因为您随后会运行到您的进程不拥有的内存中。因此,您试图使用您没有访问权限的内存,从而侵犯了内存空间
您需要将其初始化为已知大小,以便解析文件并找出其中有多少首歌曲。然后初始化数组并填充它
您最好使用stl向量,如下所示:
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
using namespace std;
struct Songs
{
string title;
string artist;
int size;
Songs() {};
};
int main ()
{
std::vector< Songs > song;
ifstream fin;
fin.open("songlist.txt")
while (fin.good())
{
song.push_back( Songs() );
fin >> song.back().title;
fin >> song.back().artist;
fin >> song.back().size;
}
fin.close();
int num_songs = song.size();
cout << "welcome to the show" << endl;
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构歌曲
{
字符串标题;
弦乐艺术家;
整数大小;
歌曲(){};
};
int main()
{
std::矢量<歌曲>歌曲;
流鳍;
财务公开(“songlist.txt”)
while(fin.good())
{
歌曲。向后推(歌曲());
fin>>song.back().title;
fin>>宋.背().艺术家;
fin>>宋背()尺寸;
}
fin.close();
int num_songs=song.size();
coutSongs[num_Songs];
num_Songs的初始化在哪里?您有:
int num_songs;
Songs song[num_songs];
这不是合法的C++。数组大小需要是编译时常量表达式。< /P>
GNU编译器有一个允许它的扩展,但即使在GNU中,用于定义数组大小的变量的值也需要有一个已定义的值
当您以未定义的大小写入数组时,最终会到达内存中不允许写入的点,或者覆盖某些内存并更改程序中其他内容的含义。访问冲突是对这种情况的完全合法响应
你应该使用C++容器,比如向量、列表或DeQue.它们可以动态增长,以适应你的文件中有很多项.
< p>你的程序不“看起来正确”,它有很多错误,详细地在其他答案中.
这是一个可以正确读取歌曲列表的程序。请注意,以下是读取文件的四种可选方法。请选择对您最有意义的方法,然后删除其他三种方法
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>
struct Song
{
std::string title;
std::string artist;
int size;
Song() : size() { }
Song(const Song& song) :
title(song.title), artist(song.artist), size(song.size) { }
Song(std::string title, std::string artist, int size) :
title(title), artist(artist), size(size) { }
};
std::istream&
operator>>(std::istream& is, Song& song) {
return is >> song.title >> song.artist >> song.size;
}
int main ()
{
std::vector< Song > songs;
std::ifstream fin;
fin.open("songlist.txt");
// You could read the songs this way:
std::copy(std::istream_iterator<Song>(fin),
std::istream_iterator<Song>(),
std::back_inserter(songs));
// Or, if you don't like std::copy, you can do this:
Song song;
while(fin >> song)
songs.push_back(song);
// Or, if you don't like operator>>(istream, Song), you can do this:
std::string artist;
std::string title;
int size;
while(fin >> artist >> title >> size)
songs.push_back(Song(artist, title, size));
// Or, if you don't like using the constructor:
while(fin >> artist >> title >> size) {
Song song;
song.artist = artist;
song.title = title;
song.size = size;
songs.push_back(song);
}
int num_songs = songs.size();
std::cout << "welcome to the show: " << num_songs << "\n";
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
结构歌曲
{
std::字符串标题;
弦乐艺术家;
整数大小;
歌曲():大小(){}
歌曲(const歌曲和歌曲):
标题(歌曲.标题),艺术家(歌曲.艺术家),大小(歌曲.大小){}
歌曲(标准::字符串标题,标准::字符串艺术家,整数大小):
头衔(头衔),艺术家(艺术家),尺码(尺码){}
};
std::istream&
运营商>>(标准::istream&is、Song&Song){
返回值为>>song.title>>song.artist>>song.size;
}
int main()
{
std::矢量<歌曲>歌曲;
std::ifstream-fin;
财务公开(“songlist.txt”);
//你可以这样读这些歌:
std::copy(std::istream_迭代器(fin),
std::istream_迭代器(),
标准:背向插入器(歌曲);
//或者,如果您不喜欢std::copy,可以执行以下操作:
宋松;
while(fin>>歌曲)
歌曲。推回(歌曲);
//或者,如果您不喜欢operator>>(istream,Song),可以执行以下操作:
弦乐艺术家;
std::字符串标题;
整数大小;
同时(fin>>艺术家>>标题>>尺寸)
歌曲。向后推(歌曲(艺术家、标题、大小));
//或者,如果您不喜欢使用构造函数:
同时(fin>>艺术家>>标题>>尺寸){
宋松;
宋。艺术家=艺术家;
song.title=标题;
宋体大小=大小;
歌曲。推回(歌曲);
}
int num_songs=songs.size();
std::cout“可能是编译器?”……也…不。因为这是C++而不是C,所以使用<代码> STD::vector < /Cord>。你现在创建了一个不确定的数组,因为<代码> NuthMangs NuMuthWangs<代码>调用UB。这里有很多问题……这里的帮助是无价之宝,我现在正在工作,谢谢大家。blessingsPlease不鼓励使用istream::good
作为循环条件。它几乎总是会创建错误代码,就像在本例中一样。(您的程序可能会在向量中创建额外的、空白的、最终的歌曲
。)更喜欢而(fin>>title>>艺术家>>大小){song.push_back(歌曲(标题、艺术家、大小));}