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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pull one character from file? 1

Status
Not open for further replies.

GoAskAlice

Programmer
Aug 31, 2006
14
GB
Gosh, this sounds so easy! But, I am having serious problems.

I wrote a file using fwrite to record 9 number sequences of 8 digits each in length. No seperators, just a long line of digits went into the file.

Now, I want to pull a single digit from the file using fread but it really wont let me do it.

I have set up an array;
int a[8];

to record 8 digits from the file.

I use;
readcount = fread(a,8,8,fp);

to read the numbers from the file.

This however, fills each array with 8 digit numbers. I have tried several different ways of doing this, such as changing the number 8 to 1, and so on, and all I get is garbage output.

I mean, I just want to be able to read 8 digits at a time from the file and have each digit indexed in an array so I can process it.

It really has got me because I also tried fgetc without any success...

Can some one PLEASE help!
 
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;

char *s = "12345678";

long t = atol(s);

printf("%ld\n",t);

fp = fopen( "dbat", "w" );

fputc(*s,fp);

fclose( fp );

fp = fopen( "dbat", "r" );
int c;
int cnt = 0;
char buffer[8];
while (cnt < 8) {
c = fgetc( fp );

buffer[cnt++] = (char) c;

}

fclose( fp );


int cent = 0;
for(cent = 0; cent < 8; cent++) {


printf("%c",buffer[cent]);

}

/*
char *st = buffer;

t = atol(st);

printf("%ld\n",t);
*/

return 0;

}

This is the code I have gotten to so far.

The first print statement outputs;
12345678
The second outputs;
1???????

I dunno why? Someone give a brother a hand.
 
It's really hard to say what's going on with all that overuse of 8 in your code - where did that come from?

Code:
int a = 1;
int b = 12345678;
fwrite( &a, sizeof a, 1, fp );
fwrite( &b, sizeof b, 1, fp );
Assuming that sizeof(int)==4 on your machine, both result in 4 bytes being written to the file regardless of the stored value in the integer.

Likewise when you read the file, you read a whole int.

Reading a single byte is the equivalent of doing something like
[tt]int temp = a & 0xff;[/tt]
You get part of the number returned, not necessarily a single decimal digit (more like 2.5 decimal digits).

--
 
*smiles* @ over use of 8

Thanks for the info. It helps a little bit.

In fact, it was just now when I kinda figured it out. Using fgetc and fputc I was trying to input a whole string "12345678" (hence the 8's in code)

But like, I realised I am only inputting one char. When it came to reading the data back, I got only 1 char back.

Also, I figured out that I have to do multiple fputc in order to write string "12345678" to the file.

It makes total sense, but the material I am working from is not helping as it seems to tell me to do wrong.
 
Well if you want strings, that you can read and write a single character at a time, then you need to be using
Code:
fprintf( fp, "%d\n", myInt );
fscanf( fp, "%d", &myInt );
ch = fgetc( fp );

But with the printed form, you need some kind of whitespace between integers to tell them apart (unless all your ints are always 8 characters long).


--
 
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
FILE *fp;
const char *s = "12345678";
long t = atol(s);
int c;
int cnt = 0;
char buffer[8];
int cent = 0;

printf("%ld\n",t);

fp = fopen( "dbat", "w" );
fputs(s,fp);
fclose( fp );

fp = fopen( "dbat", "r" );

while (cnt < 8) {
  c = fgetc( fp );
  buffer[cnt++] = (char) c;
}

fclose( fp );

for(cent = 0; cent < 8; cent++)
  printf("%c",buffer[cent]);
  printf("\n");


return 0;
}
Maybe it will help a bit. Hope you don't mind little rearrangments in code.
 
Hey, I cant express how happy that code example has made me.

Now I have a working example of fputs and fputc that actually does what it is supposed to do *big smiles*

See, I am trying to create a list of all the unique numbers within a range - its a long story - but when I tried to do it in PHP it would crash my computer. Sure, it would work with ranges 1 to 10, struggled with 16, and anything above would hang the machine. So, I had C in a Nutshell sitting on my bookcase for ages gathering dust, and decided to brush it off and have a bash at the program in C.

The part I am working on at the moment is conversions between char and int and returning a array from a function. I noticed I can return a pointer to an array but not the array itself.

I really want to get this program to work under Linux and then I will be trying to compile the same program under Windows XP. It'd be cool to have the program for both systems.

Thanks again maur3r.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top