从文本中读取行然后复制到结构中

时间:2016-04-04 02:44:26

标签: c++ file input io output

所以我理解使用while循环逐行读取文本文件的基本概念,现在我的问题是我如何读取每一行,然后将这些行的内容放入作为结构一部分的变量中?

我想出了如何将文本写入文件但不知道如何存储来自同一文件的信息,所以这是我到目前为止所做的,但它似乎不起作用:

这有效:

void save(string fileName)
{
    Container *traverser = list;
    ofstream file;
    file.open(fileName);

    while (traverser != NULL){
        file << traverser->student->getFirstName() << endl;
        file << traverser->student->getLastName() << endl;
        file << traverser->student->getGrade() << endl;
        file << traverser->student->getEdu() << endl;
        traverser = traverser->next;
    }
}

这不是

void load(string fileName)
{
    Container *traverser;
    Container* c;
    Student *s;
    string fname, lname;
    int grade;
    int edu;
    ifstream file;
    file.open(fileName);
    string line;

    while (getline(file, line)){
        file >> fname;
        file >> lname;
        file >> grade;
        file >> edu;
        s = new Student(fname, lname, grade, edu);
        c = new Container();
        c->student = s;
    }
}

这是我的语法还是我一起做错了?

该程序基本上要求学生姓名,成绩和教育然后将其存储到文本文件中,然后它应该将文本文件中的信息加载回程序(我没有&#39; t发布整个代码,因为有很多)。

谢谢!

附加信息:列表是结构(学生)的链接列表

1 个答案:

答案 0 :(得分:0)

我怀疑你的数据在一行

 public class AdapterQuestion extends RecyclerView.Adapter<AdapterQuestion.ViewQuestion>{

     private LayoutInflater mLayoutInflater;

     //this is an arrayList of questionData objects
     private ArrayList<QuestionData> data =new ArrayList<>();

     //Created the layoutInflator
     public AdapterQuestion(Context context){
          //get from context
          mLayoutInflater=LayoutInflater.from(context);
     }

     public void setBloglist(ArrayList<QuestionData> data){
         this.data =data;
         notifyItemRangeChanged(0, this.data.size());
     }

     @Override
     public ViewQuestion onCreateViewHolder(ViewGroup parent, int viewType) {
         //inflates the customQuestion view or converts it to java code
         View view= mLayoutInflater.inflate(R.layout.customquestion, null);

        //We now want to convert the View into a ViewQuestion, view Question takes
        //a view so we pass the view into view question and then return it.

        ViewQuestion holder=new ViewQuestion(view);
        return holder;
    }

    //ViewGroup parent and ViewType are not being assigned.
    @Override
    public void onBindViewHolder(ViewQuestion holder, int position) {
         //here we need to bind the data to our view, there is currently no Data!
         //We need to get the data from our JSON
         //Parameters is a ViewHolder and a Position
         //This gives us the current information object from the whole arraylist
         //data.get(position) data is the arraylist and we are getting the current position or index;
         //That current obj is of Type QuestionData

         QuestionData currentObj= data.get(position);

         //we are accessing the Inflated view, or saved view with holder
         //holder.answerText is the textView in holder. We are then taking that current object
         //We are getting the text of the current object and setting it to the AnswerText view
         holder.answerText.setText(currentObj.getMtext());
         holder.answerId.setText(currentObj.getId());
         holder.mVotes.setText(currentObj.getVotes());
         holder.mLikeButton.setTag(currentObj);
     }

    @Override
    public int getItemCount() {
         return data.size();
    }

    public class ViewQuestion extends RecyclerView.ViewHolder{
        //once we create it once the reclycer view will automatically recycle it
        private TextView answerText;
        private TextView answerId;
        private TextView mVotes;
        private LikeButton mLikeButton;

        public ViewQuestion (View itemView){
            super(itemView);
            //here we are finding the views by their ID
            answerText=(TextView)itemView.findViewById(R.id.answerText);
            answerId=(TextView)itemView.findViewById(R.id.answerId);
            mVotes=(TextView)itemView.findViewById(R.id.VoteTextView);
            mLikeButton=    (LikeButton)itemView.findViewById(R.id.heart_buttons);

            mLikeButton.setOnLikeListener(new OnLikeListener() {

                @Override
                public void liked(LikeButton likeButton) {
                    Voting vote = new Voting();
                    vote.onUpVote(convertToString(), 
                            getAdapterPosition(),ViewQuestion.this);
                    System.out.print("Adapter Position"+getAdapterPosition());
                }

                @Override
                public void unLiked(LikeButton likeButton) {
                     Voting onDown=new Voting();
                     onDown.onDownVote(convertToString(), 
                             getAdapterPosition(), ViewQuestion.this);

                }
            });
        }

        public String getVoteView(){
            String voteView=mVotes.getText().toString();
            return voteView;
        }

        public String convertToString(){
            String converted=answerId.getText().toString();
            return converted;
        }

        public int convertToInt(){
            String converted=answerId.getText().toString();
            int ConvertedInt=Integer.parseInt(converted);
            return ConvertedInt;
        }
    }
}

这可能已经完成了这项工作。 修改:您可能需要while (getline(file, line)){ istringstream lstr(line); lstr >> fname; lstr >> lname; lstr >> grade; lstr >> edu; s = new Student(fname, lname, grade, edu); c = new Container(); c->student = s; }

相关问题