473,385 Members | 1,292 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

pointer issue or something else??

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
3 1833
Banfa
9,065 Expert Mod 8TB
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
ayoung
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
tyreld
144 100+
--------------------------------------------------------------------------------------------------------

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

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

Similar topics

2
by: nifsmith | last post by:
Hi I am creating my own Queue class to learn about Queues and pointers. I have come across a question of two styles and I don't know if there are any dangers associated with them. I coded...
11
by: Sushil | last post by:
Hi Gurus I've tried to come up with a small logical example of my problem. The problem is platform specific (MIPS) which I understand should not be discussed here. So here goes my example: ...
14
by: Mirko | last post by:
Hello, I'm new to this list and to Usenet in general, so please forgive (and advice) me, if I do something wrong. Anyway. I am a bit confused, because I always thought one _should_ explicitly...
23
by: Eric J.Hu | last post by:
Hi, I have following code, want do pointer convert. It always complain: vcnvt.c: In function `main': vcnvt.c:20: warning: dereferencing `void *' pointer vcnvt.c:20: request for member `key'...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
26
by: Martin Jørgensen | last post by:
Hi, I don't understand these errors I get: g++ Persort.cpp Persort.cpp: In function 'int main()': Persort.cpp:43: error: name lookup of 'j' changed for new ISO 'for' scoping Persort.cpp:37:...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
25
by: dis_is_eagle | last post by:
Hi.I have a question on the following statement. char* a="hello"; The question is where "hello" gets stored.Is it in some static area ,stack or heap.I have observed that attempting to modify...
6
by: dtschoepe | last post by:
Hi all, Working on homework again... I've got a weird problem, I've been banging my head against the wall on what is causing it. I have a pointer to a typdef named Person. At one point in the...
50
by: arunajob | last post by:
Hi all, If I have a piece of code something like this void main(void) { char * p1="abcdefghijklmn"; ............................................. }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.