Help with readin and writing into textfiles in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinay3744
    New Member
    • Sep 2007
    • 14

    Help with readin and writing into textfiles in c++

    Hello

    I am particularly new to programming working on a mathematical analysis where I have to perform some functions on a text file.

    I have a text file in this format, it contains 3 numbers in each line of the file and I need to separate each column in different files( or arrays and peform mathematics functions on them to get the solutions I need. Can you help me with the reading and separating part of the code. Thanks

    Here is a sample of the file

    "myfile.txt "

    1 2 3
    12 22 345
    123 321 897
    ..... and so on

    I want something like A[3] = { 1, 12, 123}
    b[3] = {2, 22, 345}
    c[3] = {3,345,897}

    or 3 columns in 3 separate text files....so that I can read them and perform the mathematical functions.

    Thanks for taking time out and helpin me, I am workin on suse linux.

    Vinay
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by vinay3744
    Hello

    I am particularly new to programming working on a mathematical analysis where I have to perform some functions on a text file.

    I have a text file in this format, it contains 3 numbers in each line of the file and I need to separate each column in different files( or arrays and peform mathematics functions on them to get the solutions I need. Can you help me with the reading and separating part of the code. Thanks

    Here is a sample of the file

    "myfile.txt "

    1 2 3
    12 22 345
    123 321 897
    ..... and so on

    I want something like A[3] = { 1, 12, 123}
    b[3] = {2, 22, 345}
    c[3] = {3,345,897}

    or 3 columns in 3 separate text files....so that I can read them and perform the mathematical functions.

    Thanks for taking time out and helpin me, I am workin on suse linux.

    Vinay

    Does the file contain only 3 lines?
    You can use an vector or list for this.
    1)Open the file
    2)You can read all the elemnts into an array and then iterate to get every third element instead of maintaining 3 different arrays/vectors


    Raghuram

    Comment

    • vinay3744
      New Member
      • Sep 2007
      • 14

      #3
      Originally posted by gpraghuram
      Does the file contain only 3 lines?
      You can use an vector or list for this.
      1)Open the file
      2)You can read all the elemnts into an array and then iterate to get every third element instead of maintaining 3 different arrays/vectors


      Raghuram

      Hi

      I have written some code about this which seems to work....but I am very new to programming and I am working on something which seems quite complicated to me and hard to explain over in a forum. is is ok if I add you on hotmail or gmail or something and then maybe you can help me with this program of mine..I would be extremly grateful!

      Thanks
      Vinay

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by vinay3744
        Hi

        I have written some code about this which seems to work....but I am very new to programming and I am working on something which seems quite complicated to me and hard to explain over in a forum. is is ok if I add you on hotmail or gmail or something and then maybe you can help me with this program of mine..I would be extremly grateful!

        Thanks
        Vinay
        You can post the code here itself and i can take a look into it.
        But do explain me where u are stuck?

        Raghuram

        Comment

        • vinay3744
          New Member
          • Sep 2007
          • 14

          #5
          Code:
          #include <stdio.h>
          #include <iostream>
          #include <fstream>
          
          using namespace std;
          
          int main(void){
          
          int a[650000] , b[650000] , c[650000];
          //int x;
          std::ifstream in("myfile.txt");    
          
          for (int i=0; i<650000; ++i)    // need to use the EOF command here.
          {
             in >> a[i] >> b[i] >> c[i]; 
          }
          
          for (int i=0; i<650000;++i)
          {cout<<a[i]<<"  "<<b[i]<<"  "<<c[i]<<endl;}
          cin.get();
          
          
          
          return 0;
          }
          I have used this code to read from a big sized file containing upto 150000 rows. However, after approximately using till 65000, it says "segmentati on default".

          How do I increase the holding capacity for the array so that I can store all the numbers, and also How do I use the end of file command in this case so that I can take all the numbers in.

          Thanks for your help.

          Vinay

          Comment

          • gpraghuram
            Recognized Expert Top Contributor
            • Mar 2007
            • 1275

            #6
            Originally posted by vinay3744
            Code:
            #include <stdio.h>
            #include <iostream>
            #include <fstream>
            
            using namespace std;
            
            int main(void){
            
            int a[650000] , b[650000] , c[650000];
            //int x;
            std::ifstream in("myfile.txt");    
            
            for (int i=0; i<650000; ++i)    // need to use the EOF command here.
            {
               in >> a[i] >> b[i] >> c[i]; 
            }
            
            for (int i=0; i<650000;++i)
            {cout<<a[i]<<"  "<<b[i]<<"  "<<c[i]<<endl;}
            cin.get();
            
            
            
            return 0;
            }
            I have used this code to read from a big sized file containing upto 150000 rows. However, after approximately using till 65000, it says "segmentati on default".

            How do I increase the holding capacity for the array so that I can store all the numbers, and also How do I use the end of file command in this case so that I can take all the numbers in.

            Thanks for your help.

            Vinay

            Using an array of this size is not advisable and try to use dynamic allocation to store the data.
            You are getting segmentation fault becos ur system may not have that much resource to provide you.

            Raghuram

            Comment

            • looker
              New Member
              • Dec 2007
              • 18

              #7
              Originally posted by vinay3744
              Code:
              #include <stdio.h>
              #include <iostream>
              #include <fstream>
              
              using namespace std;
              
              int main(void){
              
              int a[650000] , b[650000] , c[650000];
              //int x;
              std::ifstream in("myfile.txt");    
              
              for (int i=0; i<650000; ++i)    // need to use the EOF command here.
              {
                 in >> a[i] >> b[i] >> c[i]; 
              }
              
              for (int i=0; i<650000;++i)
              {cout<<a[i]<<"  "<<b[i]<<"  "<<c[i]<<endl;}
              cin.get();
              
              return 0;
              }
              Vinay
              I would like you to review your design first before we are going to choose the appropriate detail design:
              You have 3 arrays of int, each of which is sized 650000
              so how much memory the program will allocate for you when you run this snippet of code : (3 * 32 * 650000)/(8 * 1024 * 1024 ) = 7.4 GB
              Do you really think you have 7.4 GB of RAM in your machine ?
              And only your program runs at the time ? it is impossible right ?

              There are actually some algorithms already suggested to resolve the big amount of data in such as case as what you stated:
              - Loading all the data from text file into the array in your apps is a good way to follow because data can be more faster to be manipulated when you works with small data, but it will impact on the size of memory when you have data much bigger like this. Example for the google web site everyone knows.
              Do you really think that when you search for a pattern, google will retrieve all the search results and allocated them in memory for you ?
              A million of results is possible for that allocating ?
              Please change your design, I don't know what do you want to do with those data. So i can not tell you any solution. Could you detail your need more ?

              Comment

              • hsn
                New Member
                • Sep 2007
                • 237

                #8
                if you explain your problem more i think i can help you much better.
                they are right you can't have an array with that size, only with dynamic arrays it will work.
                and do you really need that size any way, isn't there any other way to solve your problem without that huge size??????(thin k a bout it)


                good luck

                hsn

                Comment

                Working...