• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

Reading a Char[] from a file

I have something that has been driving me crazy.  i am making an MFC Application, but I think this is a C++ problem.  I have included some of my code and the file I am reading from.  Everything reads in ok until it gets to the (infile.getline(buffer,MAX) line of code.  I cannot get it to read the string "100 OH".

Help Please.  Ask if you don't understand.

void CXfmrView::OnFileOpen()
{
  const int MAX = 150;
  pole_struct *poleptr;
  sec_struct *secptr;
  serv_struct *servptr;
  CString filename;
  int loop, loop2, loop3;
  char buffer[MAX], buffer2[MAX], checker;

  CFileDialog OpenDlg( TRUE, "dat", "*.dat", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, NULL,                          NULL );
  if (OpenDlg.DoModal() == IDOK)
  {
      filename = OpenDlg.GetFileName();
      ifstream infile(filename);

      TEXT_DISPLAY = FALSE;
      COLOR = TRUE;
      DISPLAY_VOLTAGES = FALSE;
      start_num++;
      undo_once = 0;
      mode = 1;      // begin in draw mode
      mode_word = "DRAW";
      last_drawn = 0;
      if (FREE_VOLTAGES)
      {
        free_float_vector(volt_array,0);
      }
      sec_array[0].removeallsec();
      pole_array[0].removeallpole();
      serv_array[0].removeallserv();
      
      infile >> POLE_NUM >> SERV_NUM;
      infile >> newzoom;
            //  input xfmr information
      for (loop=1; loop <= NUM_NUBS; loop++)
      {
        infile >> xfmr.m_rectNub[loop].TopLeft().x >> xfmr.m_rectNub[loop].TopLeft().y >>                                  xfmr.m_rectNub[loop].BottomRight().x >> xfmr.m_rectNub[loop].BottomRight().y;
        infile >> xfmr.NUB_NOT_CHOSEN[loop];
      }
      infile >> xfmr.xfmr_box.TopLeft().x >> xfmr.xfmr_box.TopLeft().y >>  xfmr.xfmr_box.BottomRight().x >>                                  xfmr.xfmr_box.BottomRight().y;
      infile >> xfmr.nub_chosen >> xfmr.sec_voltage >> xfmr.xfmr_point.x >> xfmr.xfmr_point.y;

      infile.getline(buffer,MAX);
      xfmr.xfmr_type = buffer;
}

////the following is the data file

31      1
0.250000
61      176      66      186      TRUE
39      193      49      197      TRUE
22      176      27      186      TRUE
39      165      49      169      TRUE
35      173      50      188
0      1.000000      10      158
?
100 OH

//  the program is supposed to read 100 OH into a char[ ] which I then place in a CString
0
scs1
Asked:
scs1
  • 10
  • 9
1 Solution
 
nietodCommented:
What is it reading in instead?
0
 
nietodCommented:
Is that question mark actually in the data file?  If so that is the problem.
0
 
nietodCommented:
I think I know waht the problem is.  After you read the last point_y,(158) you are still on that line.  So you read the rest of THAT line into the buffer, not the next line.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
scs1Author Commented:
I did not have the ? before.  I should have taken it out before I send this question.  I had that in there when I had a loop that continually performed a getline function.  

This is what I get when I look at buffer in the debugger: some address ""
It always has the ""

When I go to the variable viewer it shows the different elements that make up buffer and it is not stuff from the data file.  It is odd characters.


0
 
scs1Author Commented:
I placed several getline functions after that line b/c I thought that was the problem, but it still did not help.

I also tried 6 get functions in a row.
0
 
nietodCommented:
You can use the ignore() function to read to the end of the line.  I believe you would need

infile.ignore(numeric_limits<int>::max(),'\n');
0
 
nietodCommented:
I thought that was it.  Does the data int he buffer change each time you do a getline()?  or does it stay the same?
0
 
scs1Author Commented:
I placed several getline functions after that line b/c I thought that was the problem, but it still did not help.

I also tried 6 get functions in a row.
0
 
scs1Author Commented:
I placed six getline functions in a row and ran the debugger.

The contents of buffer never changed!!.  It was filled with math symbols and other junk.
0
 
nietodCommented:
Is it possible that you reached the end of the file before you thought you would?  Did you look to see if the last number read in was getting the 158?  

I have to go now.  I will check in in a couple of hours.  If you don't want to wait, reject my answer and hopefully someone else will help you.
0
 
scs1Author Commented:
I placed six getline functions in a row and ran the debugger.

The contents of buffer never changed!!.  It was filled with math symbols and other junk.
0
 
scs1Author Commented:
I placed six getline functions in a row and ran the debugger.

The contents of buffer never changed!!.  It was filled with math symbols and other junk.
0
 
nietodCommented:
Did you check that the last number read in (before the getline() ) was the 158?
0
 
nietodCommented:
What code are you using to read the "TRUE"s that end the line?  Would that be?

>> xfmr.NUB_NOT_CHOSEN[loop]

Does that work?  how is NUB_NOT_CHOSEN declared?   it is not a boolean is it?  Is it a character array?



0
 
scs1Author Commented:
I am glad you wrote back.  I am having trouble reading in the boolean variable.  
xfmr.NUB_NOT_CHOSEN[loop]: this is a boolean type.  However, it does not read in "TRUE" properly.  I also tried writing a "1" or "0" to the file instead of "TRUE" and "FALSE", but it is not working either.  

How do I read in a value or string and set the boolean variable to TRUE or FALSE as appropriate???
0
 
nietodCommented:
That may be part of the original problem then.

There are a variety of ways to do do it.  You can read it in as a string.  and then see if the string is "TRUE" like

char BlnStr[6];
bool Bln;

f.getline(BlnStr,6);
Bln = strcmp(BlnStr,"TRUE") == 0;
// alternately, for more protection,
if (strcmp(BlnStr,"TRUE") == 0)
   Bln = true;
else if  (strcmp(BlnStr,"FALSE") == 0)
   Bln = false;
else
   // Do something to indicate there was an error.

0
 
nietodCommented:
An easier approach is to use a character, rather than a string.  I use "Y" (for Yes) to represent true and "N" for false.  Thus like

char BlnChr;
bool Bln;

  f >> BlnChr;
  Bln = BknChr == 'Y';
0
 
scs1Author Commented:
I am trying that right now.  I think it will work.  Thanks for the help.  I will let you know if it worked soon.
0
 
scs1Author Commented:
It worked GREAT!!!  I increased the points to 60 b/c I only had 10 more pts.

Thanks for the tremendous effort!!!!
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 10
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now