English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

#include

void read(ifstream &T) //pass the file stream to the function
{

char ch;

while(!T.eof())
{
T.get(ch);
cout << ch;
}

cout << endl << "--------" << 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??

2006-06-27 01:10:38 · 5 answers · asked by Anonymous in Computers & Internet Programming & Design

5 answers

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
#include

using namespace std;

bool Load(ifstream&);
bool ReadData(ifstream&, char[],int&);
void PrintData(const char[],int);
void clear(ifstream&);


void sort(char[],int);
void swapValues(char&,char&);
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< 3; i++)
{
if(!ReadData(file,cData,iSize))
throw "Data could not be read";
cout << iSize << endl;
PrintData(cData,iSize);
sort(cData,iSize);
PrintData(cData,iSize);
}

file.close();
}
catch(char* szError)
{
cout << endl << "Error: " << szError << endl;
exit(0x01);
}

return 0;
}

bool Load(ifstream& 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& iFS,char cData[], int& iSize)
{
try
{
iFS >> iSize;
if(iSize < 0);
else
{
clear(iFS);
for(int i=0;i < iSize;i++)
iFS.get(cData[i]);
clear(iFS);
}
throw true;
}
catch(bool bCode)
{
return bCode;
}
}

void clear(ifstream& iFS)
{
char c = '\0';
while(c!= '\n' && !iFS.eof())
iFS.get(c);
c='\0';
}


void PrintData(const char cData[], int iSize)
{
for(int i=0; i < iSize; i++)
cout << cData[i];
cout << endl;
}


void sort(char a[], int numberUsed)
{
int indexOfNextSmallest;
int index;

for (index = 0; index < numberUsed - 1; index++)
{
indexOfNextSmallest = indexOfSmallest(a, index, numberUsed);
swapValues(a[index], a[indexOfNextSmallest]);
}
}

void swapValues(char& v1, char& 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 < numberUsed; index++)
if (a[index] < min)
{
min = a[index];
indexOfMin = index;
}

return indexOfMin;
}

2006-06-27 01:30:06 · answer #1 · answered by Caus 5 · 0 0

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)

2006-06-27 03:09:28 · answer #2 · answered by Anonymous · 0 0

(>*0*)> i'm on the record!? Wow, i did not comprehend i became gonna be on it! Thnx!!! you're staggering! even if my call isn't Viktorr Anymore, I replaced it :) you'll discover it lower than my Avatar. (>^-^)> Onto your Survey! a million. What anime personality do you imagine is the most customary? (>-_-)> i imagine that is probable ...L! 2. What anime desires a stay action movie? (>^-^)> Fullmetal Alchemist! i don't comprehend why, even if it would want to be so Cool! 3. What manga desires to be grew to develop into into an anime? (>^_^)> Fairy Tail!!! That Manga is tremendous and particularly desires an Anime quickly! 4. To all Code Geass followers: What do imagine will ensue contained in the purely excellent episodes of the English Dub run? What do you imagine will take Code Geass's provide up after that is over? <(T.T<)...i will't Anwer this...i'm sorry, i'm no longer a Code Geass Fan yet. 5. call an anime for each of those letters ~ Akira ~ Naruto ~ InuYasha ~ melancholy of Haruhi Suzumiya ~ Elfen Lied 6. Do you've self belief that in case you’re an anime watcher that you should do something to help the anime marketplace once on your life? ~ (>^0^)> YESSSSS!!! that could want to nicely be a regulation/RULE/modification!!! Whoever would not attempt this...i'm gonna things you interior a pc and enable little young ones click on you with the pointy-Mouse-Clickers to lack of life!!! Mwahaha! (^0^)/...XD 7. Sorry to say that query & died before i might want to end this poll. (>T.T)> i'm sorry! If truly I were right here somewhat faster! that is all my Fault!!! *Whaaaa* (>^-^)>* ~fingers over a star~ "right here's a star on your staggering record!!!' d-(^__~)> Thumbs UP! Byezzz

2016-10-13 21:13:32 · answer #3 · answered by cohan 4 · 0 0

check if file2.txt exists OR
check permissions also OR
take different variable of ifstream OR

2006-06-27 02:03:14 · answer #4 · answered by Abhishek 3 · 0 0

Hey read it here

http://www.chris.spear.net/pli/fileio.htm

2006-06-27 01:13:07 · answer #5 · answered by Joe_Young 6 · 0 0

fedest.com, questions and answers