?
Solved

reading from file

Posted on 2003-03-16
3
Medium Priority
?
164 Views
Last Modified: 2010-04-15
I'm a 3rd year computer science student who is new to C programming and needs some help.

I can get this program to write to a file without any problems, but whenever I try to read from the file it comes back null.

Here's my code:

char *string="My dog has fleas";
char *ptr,*input[50],*output[50];
int count=1;

int main()
{

FILE *in;
FILE *out;

printf("Enter name of source file\n");
scanf("%s",&input);
printf("Enter name of output file\n");
scanf("%s",&output);

in = fopen(source,"r");        //assigns the source file to 'in' for read only
out = fopen(output,"w+");      //assigns the output file to 'out' for writing/overwriting

fputs(string,out);             //outputs the value of string to the file

fread(ptr,sizeof(*string),count,in);     //reads the file and assigns what is read to ptr
                                                                                //count is the number of characters to read
printf("%s\n",ptr);                     //prints the ptr string.

}

Interestingly, the fread() function returns 0, so it is working.  I just cant figure out why ptr is NULL in the printf() statement.  It should be equal to the first character in string (ie: "M"). My understanding is that it should be null in the fread() statement.

Unfortunately my Systems Programming teacher doesn't consider teaching to be one of his job functions, so any help is greatly appreciated.
0
Comment
Question by:CaptainSparky
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:CaptainSparky
ID: 8147237
Sorry folks, but I've solved my own question.
The solution was simply to point *ptr to *string
(ie: ptr=string;)

That one simple line did the trick.
0
 
LVL 15

Accepted Solution

by:
efn earned 100 total points
ID: 8147415
That solution may work, but it could probably be improved.

If the "ptr=string" assignment occurred before the call to fread, you are using the string constant named "string" as an input buffer.  That is, you are telling fread to write over it.  This is not a good idea.  In the words of Harbison and Steele, "You should never attempt to modify the memory that holds the characters of a string constant, since that memory may be 'read-only,' that is, physically protected against modification."  (_C:  A Reference Manual_, 4th ed., 1995, p.31).

The usual approach is to use a character array:

char inputbuffer[500];

fread(inputbuffer, size, count, stream);


0
 

Author Comment

by:CaptainSparky
ID: 8147581
Thanks very much for the suggestion.  I tried it and it works.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question