Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

c++ - STATUS_ACCESS_VIOLATION when reading file int struct

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;
}

Why does the program produce STATUS_ACCESS_VIOLATION when reading the file into a struct?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...