Solved

WRITE and READ Incorrectly!!!

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

Discover new time-saving features 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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

932 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

15 Experts available now in Live!

Get 1:1 Help Now