Thread: problem with text files

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    12

    problem with text files

    hi..
    i'm learning C++ and i have a problem that has never happened to me before:

    i have a project where i use text files.. i open the file, close it, start a FOR, open the file again (inside the FOR), and when i close the file again (inside the FOR) an error apears saying "..The memory could not be "read"".

    the weirdest thing is that when i run the program in Debug, this doesn't happen and the program does what i want!!!

    Code:
    int count;
    FILE *fin;
    char wordtxtfile[50];
    char txtfile[2][10];
    /*****************************/
    strcpy(txtfile[0],"PT.txt");
    strcpy(txtfile[1],"FR.txt");
    /*****************************/
    
    count=11;
    for (int x=0;x<2;x++)
     {
      if ((fin=fopen(txtfile[x],"r"))==NULL)
        printf ("\nError");
      else
       {
         fclose(fin);
         for (int y=0;y<=count;y++)
          {
            fin=fopen(txtfile[x],"r");
            while ((fscanf(fin,"%s",wordtxtfile))!=EOF)
             {
               printf ("%s\n",wordtxtfile);
             }
            fclose(fin);
          }
       }
    }
    Please help me! i need this problem solved quickly.. =S

    edit: i misplaced the fclose(fin)
    Last edited by Gather; 06-04-2006 at 06:10 PM.

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    I have no idea why you are trying to do here, your code seems a bit nackered to me.

    If the file opens successfully, then you go and open it up again within the else..... why is?

    Then when you are in the for loop, you open the file and then close it 11 times....

    I dont get this program in the slightest.
    Be a leader and not a follower.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    12
    Quote Originally Posted by subdene
    I have no idea why you are trying to do here, your code seems a bit nackered to me.

    If the file opens successfully, then you go and open it up again within the else..... why is?

    Then when you are in the for loop, you open the file and then close it 11 times....

    I dont get this program in the slightest.
    sry.. i didnt explain what the program does.. and i only put the important part of the program!!
    basically, it opens each file (PT.txt and FR.txt) ,the first time, to see if it exists.. then closes it and starts the for.. to print the words from the file 11 times!!

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Stop opening and closing the file over and over again. fopen returns a NULL if it fails. That's all that you need to check.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    12
    Quote Originally Posted by citizen
    Stop opening and closing the file over and over again. fopen returns a NULL if it fails. That's all that you need to check.
    there's a reason why i'm doing that.. but i'm not going to explain it now..
    the question is.. does the problem come from there?!?!

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Yes. The file's not open when you try to use fscanf in the while loop.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    12
    Code:
    fin=fopen(txtfile[x],"r");
    while ((fscanf(fin,"%s",wordtxtfile))!=EOF)
    it's not opened??

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Well, you do not do any error checking, so you really can't be sure.

    And do please note that this is the C++ forum.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    12
    Quote Originally Posted by jafet
    Well, you do not do any error checking, so you really can't be sure.

    And do please note that this is the C++ forum.
    what do you mean?.. i did the error checking in the begining

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    In your code segment I do not see how it is necessary to open and close the file with every iteration of the loop, it doesn't make sense and it's slow, and you don't do the error checking for each iteration.

    Have you considered opening the file once, reading it once, and printing the contents 11 times, as you intended? It's a lot less likely to die that way.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Gather
    the first time, to see if it exists.. then closes it and starts the for.. to print the words from the file 11 times!!
    Your code tries to open and close each file 13 times and would prints its contents 12 times
    Kurt

  12. #12
    Registered User
    Join Date
    Jun 2006
    Posts
    12
    Quote Originally Posted by citizen
    In your code segment I do not see how it is necessary to open and close the file with every iteration of the loop, it doesn't make sense and it's slow, and you don't do the error checking for each iteration.

    Have you considered opening the file once, reading it once, and printing the contents 11 times, as you intended? It's a lot less likely to die that way.
    How do i print the contents 11 times without reopening the file? so that it goes to the begining of the file at the start of every loop.

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    store the contents to a string and then close the file. Use the string to print the contents.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #14
    Registered User
    Join Date
    Jun 2006
    Posts
    12
    I cant.. =S I have to use the files.. cant use strings

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    I have to use the files.. cant use strings
    Why not?
    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. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. C: include files problem
    By threahdead in forum Linux Programming
    Replies: 4
    Last Post: 05-07-2004, 08:02 AM
  5. Problem with static text in dialog boxes
    By Clyde in forum Windows Programming
    Replies: 11
    Last Post: 05-28-2002, 12:51 PM