Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Reading a Char[] from a file

Posted on 1998-08-04
19
Medium Priority
?
238 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 240 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

610 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