Thread: Reading text file

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    3

    Reading text file

    Hallo,

    I try to read a line of text in a file that looks like this: 10N11M12R
    where N, M and R are numbers from 1 to 2000.
    For 3-digit numbers, I extracted N, M and R in this way:
    Code:
     int count = 0; 
     int ch;
      try
        {
         
            CStdioFile file(_T("temp.txt"), CFile::modeRead);
            CString str,mainstr = _T("");
     
            while(file.ReadString(str))
            {
                mainstr += str;
                mainstr += _T("\n");
            }
             
            textBox2 ->SetWindowTextA(mainstr.Mid(3,3));   // N
            textBox3 ->SetWindowTextA(mainstr.Mid(10,3)); // M
            textBox4 ->SetWindowTextA(mainstr.Mid(17,3)); // R    
          
        }
        catch(CException* e)//Catch by pointer as exceptions in MFC are crap
        {
            MessageBox(_T("Error - unable to open file"));
            e->Delete();//Lame...very lame...but needed
     
        }
    How can parse this line and determine the number of digits that contains N, M and R?

    Thanks,

    Haris
    Last edited by Haris1; 09-20-2012 at 05:46 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Id try something like this
    Code:
    nr_N = mainstr.Find('N',0);
    nr_M = mainstr.Find('M', nr_N+1) - nr_N - 1;
    nr_R = mainstr.GetLength() - ( nr_N + nr_M + 2);
    This is not tested and propably wrong too but I hope you get the idea.
    Kurt

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    How get all numbers by regex?

    should get you started on finding integers within a string.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    Thanks, I try with your Feedback

    Haris

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    Thanks all, is now working.

    The release work fine on Windows 7 but not on XP(SP3). I have installed on XP Framework 4 and installed the application.
    Wen i try to write something througth the serialport, I get write failed. I cann't debug on Xp because VC 2010 ist not installed.
    The application is a MFC in a static Library and I think isn't a dll problem. What can the problem be?

    Haris

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading From a text file
    By jian in forum C Programming
    Replies: 6
    Last Post: 12-31-2010, 04:43 AM
  2. Replies: 8
    Last Post: 05-05-2010, 02:43 PM
  3. Reading from a text file
    By pratima_t1 in forum C Programming
    Replies: 2
    Last Post: 03-24-2008, 09:24 AM
  4. Reading text file twice, can it be done once?
    By CaeZaR in forum C++ Programming
    Replies: 4
    Last Post: 02-04-2006, 04:18 PM
  5. reading from text file
    By david999 in forum C Programming
    Replies: 8
    Last Post: 11-04-2005, 05:14 PM

Tags for this Thread