STATUS_ACCESS_VIOLATION when reading file int struct(STATUS_ACCESS_VIOLATION 读取文件 int 结构时)
问题描述
我正在将 3 件事读入结构 Songs:歌曲标题、艺术家、文件大小.运行程序时出现错误,但它看起来是正确的.
I am reading 3 things into a struct Songs: songtitle, artist, size of file. I am getting a error when i run the program, tho it looks correct.
#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;
}
为什么程序在将文件读入struct
时会产生STATUS_ACCESS_VIOLATION
?
Why does the program produce STATUS_ACCESS_VIOLATION
when reading the file into a struct
?
推荐答案
你的程序看起来不正确",它有很多错误,在其他答案中有详细说明.
You program doesn't "look correct", it has a number of errors, detailed in other answers.
这是一个正确读取歌曲列表的程序.请注意,这是读取文件的四种替代方法.选择对您最有意义的一项,然后删除其他三项.
Here is a program that correctly reads in the song list. Note that these are four alternative methods for reading the file. Choose the one that makes the most sense to you and delete the other three.
#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 << "
";
return 0;
}
这篇关于STATUS_ACCESS_VIOLATION 读取文件 int 结构时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:STATUS_ACCESS_VIOLATION 读取文件 int 结构时
基础教程推荐
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01