By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

pointer issue or something else??

P: 2
I'm trying to do a 'simple' program that reads formatted text from a file, then prints to the screen, then reads text from the keyboard, and writes to a file. However, it is not reading the text from the file, or printing the contents to the screen. it is however writing the text to the textfile. Any ideas whats going wrong?

#include <stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int fdin_file; /* file descriptor */
char from_key[30];
char from_file[300];
int mode;
int count = 0;
int main(){

FILE *in_file_ptr;

fdin_file = open("Text.txt", O_RDWR, mode); /* getting file
descriptor from file */
if (fdin_file < 0){
printf("Error:Unable to open input file\n");
}

in_file_ptr = fopen("Text.txt", "r+");
if (in_file_ptr == NULL) { /*Test for error in opening file */
printf("Error: Unable to open input file\n");
exit(1);
}

fgets(from_file, 300, in_file_ptr); /* gets contents of file and
puts
them in variable from_file */


/* prints to screen the contents of text file */
printf("This is what it says in the text file: %s\n", in_file_ptr);


printf("Please type some words to be added to text file: \n");
fgets(from_key, 30, stdin); /* reads from keyboard and puts in
from_key variable */


/* writes to file the contents of variable from_key */
fprintf(in_file_ptr, "This is the what we just wrote to file: %s\n",
from_key);


/* prints to screen new contents of text file*/
printf("This is the contents of the text file now: %s", in_file_ptr);
Sep 29 '06 #1
Share this Question
Share on Google+
3 Replies


Banfa
Expert Mod 5K+
P: 8,916
I can think of 3 possibilities

1. There is nothing in the text file

2. While there is something in the text file there is nothing on the first line (fgets reads 1 line at a time)

3. Mode r+ positions the file pointer at the end of the file so when you read there is nothing left in the file because you are reading from the end. Use fseek to position the file pointer at the place you want to read and then at the place you want to write.
Sep 29 '06 #2

P: 2
I can think of 3 possibilities

1. There is nothing in the text file

2. While there is something in the text file there is nothing on the first line (fgets reads 1 line at a time)

3. Mode r+ positions the file pointer at the end of the file so when you read there is nothing left in the file because you are reading from the end. Use fseek to position the file pointer at the place you want to read and then at the place you want to write.
--------------------------------------------------------------------------------------------------------

I've just looked up the fseek command (as i can view text in file, and there is text on the first line), however I've come across a statement saying it can't be used on file pointers returned by fopen(). Is there a way around this?
Sep 29 '06 #3

100+
P: 144
--------------------------------------------------------------------------------------------------------

I've just looked up the fseek command (as i can view text in file, and there is text on the first line), however I've come across a statement saying it can't be used on file pointers returned by fopen(). Is there a way around this?
Where did you read this statement? Here is an example that clearly uses fopen:

Expand|Select|Wrap|Line Numbers
  1. /* fseek example */
  2. #include <stdio.h>
  3.  
  4. int main ()
  5. {
  6.   FILE * pFile;
  7.   pFile = fopen ("myfile.txt","w");
  8.   fputs ("This is an apple.",pFile);
  9.   fseek (pFile,9,SEEK_SET);
  10.   fputs (" sam",pFile);
  11.   fclose (pFile);
  12.   return 0;
  13. }
  14.  
  15. // After this code is executed, a file called example.txt will be created 
  16. // and will contain the sentence:
  17. //
  18. // This is a sample.
  19.  
Sep 29 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.