Solved

Reading a Char[] from a file

Posted on 1998-08-04
19
235 Views
Last Modified: 2010-04-02
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
Comment
Question by:scs1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 9
19 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1169396
What is it reading in instead?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169397
Is that question mark actually in the data file?  If so that is the problem.
0
 
LVL 22

Accepted Solution

by:
nietod earned 60 total points
ID: 1169398
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!

 

Author Comment

by:scs1
ID: 1169399
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
 

Author Comment

by:scs1
ID: 1169400
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169401
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169402
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
 

Author Comment

by:scs1
ID: 1169403
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
 

Author Comment

by:scs1
ID: 1169404
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169405
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
 

Author Comment

by:scs1
ID: 1169406
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
 

Author Comment

by:scs1
ID: 1169407
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169408
Did you check that the last number read in (before the getline() ) was the 158?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169409
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
 

Author Comment

by:scs1
ID: 1169410
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169411
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169412
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
 

Author Comment

by:scs1
ID: 1169413
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
 

Author Comment

by:scs1
ID: 1169414
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Add values of each row in an array 3 71
C++ mouse_event mouse look 7 112
Android development question 2 81
FMX TEdit KeyUp handler detecting  "enter" key 4 41
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question