Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

WRITE and READ Incorrectly!!!

Posted on 2000-05-11
5
Medium Priority
?
203 Views
Last Modified: 2010-04-15
I have one simple program to write in a File, BUT write in the file  TRASH!!!  write (Edited by Computer101)!!

why???

this is my source code:
//-----------------------------------
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

main(){

FILE *fp;

 char ch;
 int i;
 clrscr();
 if((fp= fopen("zzz.txt", "w")) == NULL){
        gotoxy(10,10);printf("Error!!");
        exit(1);  
 }
 i=10;
 do{
   gotoxy(10,i);
   ch= getchar();
   putc(ch, fp);
   i++;
 }while(ch!='\n');
  fclose(fp);  
 return(0);
}

Now I have a program to read the file BUT Read Transh! and (Edited by Computer101)!!
why??

this is my source code:
//-------------------------------------------------------------
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
main(){
 FILE *fp;
 char ch;
 int i;
 clrscr();
 if((fp= fopen("zzz.txt", "r")) == NULL){
     gotoxy(10,10);printf("Error");
     exit(1);  
 }
 else{
  i=10;
 do{
  gotoxy(10,i);
  ch= getc(fp);
  putchar(ch);
  i++;
 }while(ch!= EOF);
 fclose(fp);
};

return(0);
}



Thanks again for all!!!!

regards

0
Comment
Question by:nowait
[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
5 Comments
 
LVL 10

Expert Comment

by:makerp
ID: 2803632
ive tested it in VC++ 5 and its fine. the only thing i had to take out was the gotoxy's and clrscr as they are not supported in conio.h..when ever i used to use gotoxy i always used cout<< or printf not putc, whever this makes any difference i dont know ?. what ive put bellow should be fine in borland and VC++.

write to file :

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

main()
{

FILE *fp;

 char ch;
 int i;

 if((fp= fopen("d:\\examples\\c_test_area\\zzz.txt", "w")) == NULL){
        printf("Error!!");
        exit(1);  
 }
 i=10;
 do{  
   ch= getchar();
   putc(ch, fp);
   i++;
 }while(ch!='\n');
  fclose(fp);  
 return(0);
}

read from file :
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>

main(){
 FILE *fp;
 char ch;
 int i;

 if((fp= fopen("d:\\examples\\c_test_area\\zzz.txt", "r")) == NULL){
     printf("Error");
     exit(1);  
 }
 else{
  i=10;
 do{

  ch= getc(fp);
  putchar(ch);
  i++;
 }while(ch!= EOF);
 fclose(fp);
};

return(0);
}

0
 

Expert Comment

by:sa9813
ID: 2803634
Your code works fine for me!
0
 

Expert Comment

by:haren
ID: 2806253
try using getch() and putch().
0
 
LVL 3

Accepted Solution

by:
sburck earned 300 total points
ID: 2807989
The only thing I can see offhand is the undeclared file open mode, try opening and closing the files with "r+t" and "w+t".
0
 

Author Comment

by:nowait
ID: 2813478
thanks for all!!
I use yours sugestions...
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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

721 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