Thread: Problem with Reading Input File

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    Question Problem with Reading Input File

    Hi,

    I am having problem with getting input from text file and display the output in another text file. My coding works like this. First it reads input from text file and store them in struct variable using array. Then, by using the same variable, it will write the output in a text file. I presume the problem is the code did not work well when reading the input. Thus, it produces incorrect result. So here, I attach the input file, codes, and the produced output. Pls help me fixing this cause I have tried the whole day but to no avail.Thanks!

    Input file : courseList.txt
    201000101 Christopher Ng; CSC110; 1 CTU101; BEL120; CSC128; CSC118; MAT183;
    201100210 James Woo; CSC113; 2 MAT183; CSC128; BEL130; MAT112; CTU103;
    201000311 Nurul Izah Jabbar; CSC110; 1 CTU101; BEL120; CSC128; CSC118; MAT183;
    201000548 Nadia Akmal Ali; CSC110; 3 CTU211; BEL311; CSC238; CSC253; MAT183;
    201105253 Azizul Hakim Jamal; CSC110; 4 CSC204; CSC248; CSC318; ITS232; MAT183;


    Code:
    struct Course
    {
        int studentID;
       char name[30];
       char progCode[10];
       int semester;
       char courseCode[5][10];
    };
    
    int main()
    {
        ifstream in;
       ofstream out1, out2, out3;
       Course course[5];
    
       in.open("courseList.txt");
       out1.open("CSC110.txt");
       out2.open("CSC113.txt");
       out3.open("CSC110MAT183.txt");
    
        //(a)
       if (in.fail())
       {
           cout << "File does not exist." << endl;
          return 0;
       }
    
       //(b)
       for (int i=0; i<5; i++)
       {
           in >> course[i].studentID;
          in.getline(course[i].name,29,';');
          in.getline(course[i].progCode,6,';');
          in >> course[i].semester;
          for (int j=0; j<5; j++)
              in.getline(course[i].courseCode[j],6,';');
       }
    
       for (int i=0; i<5; i++)
       {
           cout << course[i].studentID;
          cout << course[i].name;
          cout << course[i].progCode;
          cout << course[i].semester;
          for (int j=0; j<5; j++)
              cout << course[i].courseCode[j];
          cout << endl;
       }
    
       in.close();
       out1.close();
       out2.close();
       out3.close();
       getch();
       return 0;
    
    }
    Output:
    201000101 Christopher Ng CSC110 1 CTU101 BEL120
    00
    208995052034078760
    18337510400
    -12089922080
    Last edited by Rinas; 02-10-2012 at 10:23 AM.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Cross-posted

    And seriously. Take a look at the code within your post. It's *all on one line*! You need to fix that before people are going to spend any time (if they do at all, given your lack of consideration as exhibited by your cross-posting) on this problem.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    I have fixed that.. It just happened after i copy-pasted that code.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I would suggest you look at some of your "magic" numbers. In the following snippet you have defined courseCode with a size to hold 10 characters. When you go to retrieve this information why are you only trying to retrieve 6 characters (this is one of the magic numbers)?
    Code:
    char courseCode[5][10];
    in.getline(course[i].courseCode[j],6,';');
    How many characters do you think "CSC110" contains?

    Edit: You should also seriously consider using std::string instead of the C-strings.

    Jim
    Last edited by jimblumberg; 02-10-2012 at 11:11 AM.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Code:
    for (int i=0; i<5; i++)
       {
           in >> course[i].studentID;
          in.getline(course[i].name,29,';');
          in.getline(course[i].progCode,6,';');
          in >> course[i].semester;
          for (int j=0; j<5; j++)
          {
              in.getline(course[i].courseCode[j],6,';');
          }
       }
    I have changed the codes. But, it still produce the same error. Pls help.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Post your current code, plus post the contents of any error messages exactly as they appear in your development environment.

    I also still need an answer to the question I asked in my last post:

    How many characters do you think the string "CSC110" contains?

    Jim
    Last edited by jimblumberg; 02-20-2012 at 09:31 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from an input file
    By Trevers in forum C Programming
    Replies: 1
    Last Post: 05-06-2010, 12:21 PM
  2. Reading Input from file
    By Horox in forum C Programming
    Replies: 2
    Last Post: 01-31-2008, 12:13 AM
  3. reading input file
    By dellebelle751 in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2002, 02:05 PM
  4. reading input from file
    By lavon in forum C Programming
    Replies: 1
    Last Post: 03-19-2002, 06:49 PM
  5. Reading input from a file
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-19-2002, 06:34 PM