Solved

WRITE and READ Incorrectly!!!

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now