Thread: how to use fstream library

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    33

    how to use fstream library

    have an assignment to read 10000 files from a file name"10000ia" and then after applying selection sort on it it should be print on another file say"10000sorted".what i have done i include fstream library

    Code:
    ifstream ifile;
    
    ifile.open("10000ia.txt",ios::in);
    
    ifile.seekg(0);
    
    ifile>>;
    
    ifile.close();
    
    ofstream file;
    
    file.open("10000.txt",ios::out);
    
    file<<selection(a);
    
    file.close();
    
    system("pause");
    
    return 0;
    
    }
    
    
    int selection(int a[10000])
    {
        int i,excount=0,comcount=0;
    
    int min;
    
    for( int i=0;i<10000;i++)
    {
         min=i;
         
         for (int j=i+1;j<10000;j++)
         
         {
             if (a[min]>a[j])
             {
                   min=j;
                            
             }   
         
         }
             
                 int temp=a[i];
                     a[i]=a[min];
                     a[min]=temp;
                     comcount=(15*(15+1))/2;
                     excount=excount+1;
             
                      
           
         
    
    }
    
    
                              
    
    cout<<"Number of exchange are:"<<excount<<"\n";
    cout<<"Number of comparisons are:"<<comcount<<"\n";
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    33
    i dont know that what to write in file>>????

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    This should go to the C++ forum

    But basically the idea is
    stream >> buffer
    So you'll need some sort of buffer to direct the file information into

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to C++ Programming forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    33
    how could i set the 10000 numbers into an array how i code it that all numbers
    sets in an array for example a[1..10000]=all numbers in file named"10000ia.txt"...

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by salmanriaz
    how could i set the 10000 numbers into an array how i code it that all numbers
    sets in an array for example a[1..10000]=all numbers in file named"10000ia.txt"...
    Just a moment...

    Firstly, do you know how to read a single number from the file into a variable? Then, can you be sure that there will be no more than 10000 numbers to read in, or is there a variable number of numbers such that a fixed upper bound is inappropriate?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    33
    as far as i know if i assign a particular number to a variable for example:
    int x=1 then i can easily read it(x) from using fstream function (ifstream) but if there are only numbers given in the list then how could i read them as they r not assigning to any variable.
    as far as ur question about the numbers then yes there are constant 10000 numbers in the list..

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by salmanriaz
    as far as i know if i assign a particular number to a variable for example:
    int x=1 then i can easily read it(x) from using fstream function (ifstream) but if there are only numbers given in the list then how could i read them as they r not assigning to any variable.
    Sorry, but I do not understand what you are saying. Maybe this will help: write a program to read in an integer from standard input, then print the integer read to standard output. Show us your code for this program, then we will show you how to transform it to read an integer from a file.

    Quote Originally Posted by salmanriaz
    as far as ur question about the numbers then yes there are constant 10000 numbers in the list..
    In that case an array will be good enough, but if you know how to use a std::vector<int>, then you could still use that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's an import library?
    By chiefmonkey in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2009, 05:00 PM
  2. Property Set Library (PSL) - Announcement
    By vultur_gryphus in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 05-29-2008, 06:04 AM
  3. Makefile for a library
    By sirmoreno in forum Linux Programming
    Replies: 5
    Last Post: 06-04-2006, 04:52 AM
  4. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM
  5. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM