1

I am trying to copy contents of a file into fields in a class courseInfo. this is the code im using:

#include<iostream>
#include<fstream>
#include<vector>
#include<sstream>
#include <bits/stdc++.h> 

using namespace std;
class courseInfo
{
public:
   char courseCode[8];
   char courseName[80];
   int ECTS;
};

int main()
{
   ifstream fin("courses.txt");
   if(!fin.is_open())
   {
      cout<<"file doesn't exist";
      return 0;
   }
   string line;
   vector<courseInfo> courses;
   while(getline(fin,line))
   {
      stringstream linestream(line);
      string segment;
      vector<string> segmentlist;
      while(getline(linestream, segment, ';'))
      {
         segmentlist.push_back(segment);
      }
     //cout<<segmentlist.at(0).c_str();
      courseInfo c;
      //segmentlist.at(0).copy(c.courseCode, segmentlist.at(0).size()+1);
      //c.courseCode[segmentlist.at(0).size()] = '\0';
      strcpy(c.courseCode, segmentlist.at(0).c_str());
      cout<<c.courseCode<<"\n;
      strcpy(c.courseName, segmentlist.at(1).c_str());
      cout<<c.courseCode;
    }
   return 0;
  }

content of courses.txt file:

TURK 101;Turkish l;3.

output i get:

TURK 101

TURK 101Turkish l

the contents of courseCode changes when i copy something into courseName. why does this happen? how do i rectify this?

3
  • 1
    Because the arrays you declared are not big enough. TURK 101 is eight characters so needs an array of size 9 to store it. This is because C style strings have an extra nul character at the end. Commented Mar 27, 2020 at 9:50
  • its working now @john Commented Mar 27, 2020 at 9:54
  • 1
    Since you''re using C++, add #include <string> header and replace your char array with std::string, and use std::copy(segmentlist.begin(), segmentlist.end(), c.courseCode.begin() ) instead of strcpy Commented Mar 27, 2020 at 9:56

1 Answer 1

1

Note how TURK 101 is exactly 8 bytes. When you cout << c.courseCode, your program prints characters until it encounters a NUL byte. By accident, the first byte of c.courseName is NUL. After you read into it, it is no longer NUL and thus printing c.courseCode happily continues into c.courseName.

Some options:

  • The most obvious (and recommended) solution is to use std::string in your struct instead of fixed-size char arrays. However, this looks like a homework question, so you probably are not allowed to use std::string.
  • Use std::vector<char> instead, but that is probably also not allowed.
  • Make courseCode large enough to contain any possible course code, plus one character for the NUL-terminator. In this case, make courseCode 9 chars large.
  • Use heap-allocated memory (new char[str.size()+1] to allocate a char *, delete[] ptr to free it afterwards). And then change courseInfo to take regular pointers. Ideally all the memory management is done in constructors/destructors. See the rule of three/five/zero.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.