DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2006
    Posts
    7

    Handling with files

    Hi!
    I can't understand what is wrong to my code:

    #include <fstream>

    using namespace std;

    int main() {
    char c, inputString[10000];
    int i=0;

    ifstream infile("in.txt");
    while (infile.get(c)) inputString[i++]=c;
    infile.close();

    infile.open("out.txt");
    if (!infile.good()) return 0;
    system("Pause");
    return 0;
    }

    The problem is that this program can not open file "out.txt" (such file exists on disc) and so returns at line `if... return 0`. If I remove line `while...` from the code, everything works fine (file "out.txt" can be opened). Why is it so??

  2. #2
    Join Date
    May 2006
    Posts
    176
    I think the proper way for checking for opening errors is:
    Code:
    infile.open("c:\\2.txt");
    if(! infile.is_open()) return 0;
    I hope this helps.
    Last edited by Viorel; 12-29-2006 at 10:11 AM.

  3. #3
    Join Date
    Dec 2006
    Posts
    7
    OK, this helps in this situation which I made as small as possible, but not in my real situation.. Now I have such code:

    #include <fstream>
    #include <iostream>

    using namespace std;

    int main() {
    char c, inputString[10000];
    int i=0;

    ifstream infile("in.txt");
    ofstream outfile("out.txt");
    for (int j=0;j<10000;j++) inputString[j] = 0;
    while (infile.get(c)) inputString[i++]=c;
    outfile << inputString;
    outfile.close();
    infile.close();

    infile.open("out.txt");
    if (!infile.is_open()) return 0;
    outfile.open("res.txt");
    for (int j=0;j<10000;j++) inputString[j] = 0;
    while (infile.get(c)) {
    cout <<c;
    inputString[i++]=c;
    }

    outfile <<inputString;
    infile.close();
    outfile.close();
    system("Pause");
    return 0;
    }

    And now the problem is as follows - when cheching if infile is open, everything is ok, infile is open and we go further. But we never go into this second while loop.. althouht there are some simbols written in file "out.txt". I think maybe it thinks we have already reached end of infile.. but how can that be, we have just opened this file.. The correct action would be going into this second while loop and printing out one by one all characters in file "out.txt".

  4. #4
    Join Date
    Dec 2006
    Posts
    7
    OK, I found a solution - I had to write "infile.clear()" before reopening it.

Similar Threads

  1. Replies: 5
    Last Post: 08-01-2006, 06:42 PM
  2. XML files vs Database
    By Mansoor in forum Enterprise
    Replies: 2
    Last Post: 09-10-2002, 10:38 PM
  3. XML files vs Database
    By Mansoor in forum XML
    Replies: 8
    Last Post: 08-23-2002, 04:35 PM
  4. Replies: 0
    Last Post: 03-13-2001, 01:54 PM
  5. Replace In Files utility released
    By René Whitworth in forum vb.announcements
    Replies: 1
    Last Post: 04-29-2000, 02:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links