Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with text files

Status
Not open for further replies.

TGather

Programmer
Jun 4, 2006
6
PT
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!!!


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
{
for (int y=0;y<=count;y++)
{
fin=fopen(txtfile[x],"r");fclose(fin);
while (((fscanf(fin,"%s",wordtxtfile))!=EOF)
{
printf ("%s\n",wordtxtfile);
}
fclose(fin);
}
}
}

Please help me! i need this problem solved quickly.. =S
 
TGather,

It looks like you are opening the first file then trying to open it again before closing it.

You need the fclose(fin) before you call the second fopen.

Apart from that though you are also trying to do an fscanf after you have called fclose. This will never work.

You need to do the following:

1. Call fopen
2. Call fclose if you must before reopening (you dont really need to open and close twice)
3. In the second for loop call fopen.
4. Then remove the fclose
5. perform the fscanf until end of file
6. call fclose.

The above steps would make the code work but why do you want to open and read the same file 10 times????
 
uuupppsss..
I put the fclose(fin) in the wrong place..
well.. let me edit it..
 
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);
}
}
}

yes.. i'm doing that 10 times.. but just ignore that.. =X
 
Does that new version work?

Apart from the fact that you have no spaces anywhere, which makes it a little hard to read; it looks like that code should run. I'm still not sure what you're trying to do with it though?
 
i'm just trying to print the words from the file 10 times!!
and the program works but only in Debug.. =S
 
Please use [code][/code] tags when posting code.

--
 
TGather,

Try this line in place of your fscanf line:

Code:
while (((fscanf(fin,"%50s",wordtxtfile))!=EOF)

This will limit the number of characters copied into wordtextfile to the size of the character array. It may be that you are going past the array size.
 
th problem seems to be in the second fclose(fin)!!
 
problem solved.. =X
i needed to fflush() the file before closing it!! =P
 
> i needed to fflush() the file before closing it!! =P
That doesn't make any sense.
fflush() only has an effect when you have a file open for writing, or for update. You only have it open for reading.



--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top