Tuesday, July 14, 2009

Plz help reg FILE I/O in c++?

#include %26lt;fstream.h%26gt;





void read(ifstream %26amp;T) //pass the file stream to the function


{





char ch;





while(!T.eof())


{


T.get(ch);


cout %26lt;%26lt; ch;


}





cout %26lt;%26lt; endl %26lt;%26lt; "--------" %26lt;%26lt; endl;


}





void main()


{


ifstream T("file1.txt");


read(T);


T.close();





T.open("file2.txt");


read(T);


T.close();


}


HERE FILE1 And FILE2 ARE TWO TXET FILES.FILE 1 IS GETTING OPENED BUT FILE2 IS NOT.


WATS THE PROBLEM??

Plz help reg FILE I/O in c++?
try this for an example, you will have to modify the code, I won't do your homework for you, but this should help you out








#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;





using namespace std;





bool Load(ifstream%26amp;);


bool ReadData(ifstream%26amp;, char[],int%26amp;);


void PrintData(const char[],int);


void clear(ifstream%26amp;);








void sort(char[],int);


void swapValues(char%26amp;,char%26amp;);


int indexOfSmallest(const char a[], int startIndex, int numberUsed);








int main()


{


ifstream file;


int iSize;


char cData[100];





try


{


if(!Load(file))


throw "File Handle could not be acquired";





for(int i=0; i%26lt; 3; i++)


{


if(!ReadData(file,cData,iSize))


throw "Data could not be read";


cout %26lt;%26lt; iSize %26lt;%26lt; endl;


PrintData(cData,iSize);


sort(cData,iSize);


PrintData(cData,iSize);


}





file.close();


}


catch(char* szError)


{


cout %26lt;%26lt; endl %26lt;%26lt; "Error: " %26lt;%26lt; szError %26lt;%26lt; endl;


exit(0x01);


}





return 0;


}





bool Load(ifstream%26amp; iFS)


{


try


{


iFS.open("c:\test.txt",ios::in);


if(!iFS.is_open())


throw false;


throw true;


}


catch(bool bCode)


{


return bCode;


}


}





bool ReadData(ifstream%26amp; iFS,char cData[], int%26amp; iSize)


{


try


{


iFS %26gt;%26gt; iSize;


if(iSize %26lt; 0);


else


{


clear(iFS);


for(int i=0;i %26lt; iSize;i++)


iFS.get(cData[i]);


clear(iFS);


}


throw true;


}


catch(bool bCode)


{


return bCode;


}


}





void clear(ifstream%26amp; iFS)


{


char c = '\0';


while(c!= '\n' %26amp;%26amp; !iFS.eof())


iFS.get(c);


c='\0';


}








void PrintData(const char cData[], int iSize)


{


for(int i=0; i %26lt; iSize; i++)


cout %26lt;%26lt; cData[i];


cout %26lt;%26lt; endl;


}








void sort(char a[], int numberUsed)


{


int indexOfNextSmallest;


int index;





for (index = 0; index %26lt; numberUsed - 1; index++)


{


indexOfNextSmallest = indexOfSmallest(a, index, numberUsed);


swapValues(a[index], a[indexOfNextSmallest]);


}


}





void swapValues(char%26amp; v1, char%26amp; v2)


{


char temp;


temp = v1;


v1 = v2;


v2 = temp;


}





int indexOfSmallest(const char a[], int startIndex, int numberUsed)


{


int min = a[startIndex],


indexOfMin = startIndex;


int index;





for (index = startIndex + 1; index %26lt; numberUsed; index++)


if (a[index] %26lt; min)


{


min = a[index];


indexOfMin = index;


}





return indexOfMin;


}
Reply:check if file2.txt exists OR


check permissions also OR


take different variable of ifstream OR
Reply:Hey read it here





http://www.chris.spear.net/pli/fileio.ht...
Reply:I just spent a while messing round with this and it took me a bit of time, but i figured out what's gone wrong. As far as I can tell the "close" method, doesn't completely reset the ifstream class, so when you open a new file with the same ifstream instance, it still has its "eof" bit set. You can fix it, by adding:





T.clear();





After your first call to:





T.close();





At least, that fixes it on my version (C++.Net 2003)


No comments:

Post a Comment