Mail Archives: djgpp/2000/05/02/01:09:31
On Tue, 02 May 2000 03:32:04 Sxott b. wrote:
>I want to read 2 values from a text file, modify them and write them back.
>I placed them alone on a line each so the file looked like this
>
>12
>45
>
>
>that was all, but I couldn't figure out how, so I wrote the text file as
>
>a12
>c45
>
>that way I could search for a and take the number following, and likewise
>with c, but none of these things worked, is there a way to do this? how
>would I go about it thanks.
>if ((fp = fopen("c:\\text.txt","rt")) == NULL)
> {
> printf("Error opening file text.txt\n");
> }
> if(write!=1)
> {
> //here is the place to read in added and counted
> //dos_read fread(added_int,size?,1,fp); these are wrong
> //fread(counted_int,size?,1,fp);
> for(i=0;(c=fgetc(fp))!=EOF&&i<1000;++i)
> data[i]=(char)c;
> for(c=0;c<i;++c)
> if(data[c]=='a'){added_int=data[c+1];}
> if(data[c]=='c'){counted_int=data[c+1];}
> }
>fclose(fp);
Hmmm... judging from your comments, you're using C++.
Maybe you should try using C++ file streams instead.
You can use them just like you would use `cout' and
`cin' C++ streams (ie take input with `<<' and output
with `>>').
Or have tried replacing `fread' with `fscanf'? It
works just like `scanf' only it works with *open*
files. `scanf("%d", %data)' is also
`fscanf(stdin, "%d", &data)'. `fread' is best used
for reading in large chunks of a file opened in
binary mode. In your case, `fscanf' would work since
your separators are newlines.
Have fun!
---
nimrod_a_abing
----------------
Hot Windows Tip:
----------------
How to free up at least 300 MB of hard disk
space (as seen from the MS-DOS prompt):
C:\>deltree c:\windows
Delete directory "Windows" and all its subdirectories? [yn] y
Deleting Windows...
C:\>
(don't try this at home kids... not unless
you have Linux installation CDs)
Join 18 million Eudora users by signing up for a free Eudora Web-Mail account at http://www.eudoramail.com
- Raw text -