4 Replies - 683 Views - Last Post: 18 June 2012 - 09:23 AM Rate Topic: -----

#1 cry4you  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 09-October 09

C++ file read as integer

Posted 18 June 2012 - 07:52 AM

Hello.

I have to read text file with structure:
nummber;nummber;text;
nummber;nummber;text;
nummber;nummber;text;

10;20;first;
21;30;second;
31;40;third;

Here is printscreen of file structure: http://prntscr.com/arexa

So when i enter an integer then it calculate in witch range this integer is. For example if i enter - 34. Then function will return Third. And it have to be done without using String datatype.

I would like to ask, how can i read an integer until i meet ';' symbol and then compare. Is there any kind of easy solution in this case.

Regards,
John

Is This A Good Question/Topic? 0
  • +

Replies To: C++ file read as integer

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4131
  • View blog
  • Posts: 12,844
  • Joined: 25-December 09

Re: C++ file read as integer

Posted 18 June 2012 - 08:11 AM

You need to show some code. Do you know how to open a file? Do you know how to read a line of text?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 cry4you  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 09-October 09

Re: C++ file read as integer

Posted 18 June 2012 - 08:33 AM

This is how far i have been done.
Wrote and split file content in char. And now i am about to convert them to Integer and compare.

int main(){


ifstream::pos_type size;
char * memblock;


  ifstream file ("file.txt", ios::in|ios::binary|ios::ate);
  if (file.is_open())
  {
    size = file.tellg();
    memblock = new char [size];
    file.seekg (0, ios::beg);
    file.read (memblock, size);
    file.close();

    char * pch;
     pch = strtok (memblock,";");
    
  while (pch != NULL)
  {         int raise = 1, numar[16];
  
             int numToInt = 0;
           for (int i = strlen(pch) - 1; i > -1 ; i--) 
          {
           char temp = pch[i];
           numar[i] = atoi(&temp);   
           numToInt += numar[i] * raise;
           raise *= 10;
           }
           
    cout<<numToInt<<endl;
    pch = strtok (NULL, ";");
  }


    delete[] memblock;
  }
  else cout << "Unable to open file";
  
 system("pause");
 return 0;
    
}

This post has been edited by cry4you: 18 June 2012 - 08:34 AM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4131
  • View blog
  • Posts: 12,844
  • Joined: 25-December 09

Re: C++ file read as integer

Posted 18 June 2012 - 08:57 AM

First a couple of comments. Since your file does not appear to be a binary file, don't open it in binary mode. Since each number is separated by a delimiter you can use the extraction operator>> to extract the numbers into your variables. Example: If your input file contains:
10;20;first;
21;30;second;
31;40;third;


#include <iostream>
using namespace std;

int main()
{
   ifstream ins("YourFile.txt");
   int num1, num2; 
   char throwAway;  // Character to dispose of the semicolon.
   char yourString[100];
   while(ins) // Only if the file opened correctly, and has more data.
   {
      ins >> num1 >> throwAway >> num2 >> throwAway;
      ins.getline(yourString,100,';');
      ins >> throwAway;
      cout << num1 << "   " << num2 << "   " << yourString << endl;
   }
   return(0);
}



Notice you don't need strtok() or atoi().


Jim
Was This Post Helpful? 1
  • +
  • -

#5 cry4you  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 09-October 09

Re: C++ file read as integer

Posted 18 June 2012 - 09:23 AM

View Postjimblumberg, on 18 June 2012 - 09:57 AM, said:

First a couple of comments. Since your file does not appear to be a binary file, don't open it in binary mode. Since each number is separated by a delimiter you can use the extraction operator>> to extract the numbers into your variables. Example: If your input file contains:
10;20;first;
21;30;second;
31;40;third;


#include <iostream>
using namespace std;

int main()
{
   ifstream ins("YourFile.txt");
   int num1, num2; 
   char throwAway;  // Character to dispose of the semicolon.
   char yourString[100];
   while(ins) // Only if the file opened correctly, and has more data.
   {
      ins >> num1 >> throwAway >> num2 >> throwAway;
      ins.getline(yourString,100,';');
      ins >> throwAway;
      cout << num1 << "   " << num2 << "   " << yourString << endl;
   }
   return(0);
}



Notice you don't need strtok() or atoi().


Jim



Thanks alot! Now its very easy to solve my problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1