Solved

Reading a Char[] from a file

Posted on 1998-08-04
19
231 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
  • 10
  • 9
19 Comments
 
LVL 22

Expert Comment

by:nietod
Comment Utility
What is it reading in instead?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
 

Author Comment

by:scs1
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Did you check that the last number read in (before the getline() ) was the 158?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now