Solved

WRITE and READ Incorrectly!!!

Posted on 2000-05-11
5
199 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
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 100 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

827 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