Solved

Reading a Char[] from a file

Posted on 1998-08-04
19
233 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
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

831 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