Solved

WRITE and READ Incorrectly!!!

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
undefined reference to `bswap_128' 9 186
C hashtable library 3 107
How to jump to matching brace in eclipse editor ? 1 353
Assignment from incompatible pointer type? 2 34
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

735 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