| | |
Not able to write the values in the file
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2009
Posts: 3
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<stdlib.h> #include<iostream.h> main() { FILE *fp,*fp1; int numbers[30]; char *buffer; int i=0,j; // To read the file fp=fopen("PR1.txt","r"); if(fp==NULL){ printf("Error: can't access file.c.\n"); exit(1); } else { printf("File opened successfully.\n"); for(i=0;i<10;i++) { // while(!feof(fp)) { /* loop through and store the numbers into the array */ printf("Number- %d", i); fscanf(fp,"%d",&numbers[i]); // i++; } } printf("Number of numbers read: %d\n\n", i); printf("The numbers are:\n"); for(j=0 ; j<i ; j++) /* now print them out 1 by 1 */ { printf("%d\n", numbers[j]); } fclose(fp); // To write the values to another file fp1= fopen("PR2.txt","w"); if(fp1=NULL) { printf("Error: can't access file.c.\n"); exit(1); } else { printf("File opened successfully.\n") ; for(i=0;i<10;i++) { fprintf(fp1,"%d\n",&numbers[i]); // fwrite(&numbers[i],sizeof(numbers[i]),10,fp1); } } fclose(fp1); cin.get(); }
Last edited by Narue; Jan 30th, 2009 at 11:05 am. Reason: added code tags
•
•
Join Date: Aug 2008
Posts: 13
Reputation:
Solved Threads: 1
You are doing a bit mistake over here...........
fprintf(fp1,"%d\n",&numbers[i]);
please don't give '&' before 'numbers[i]'
if u still face problem then just see my program
fprintf(fp1,"%d\n",&numbers[i]);
please don't give '&' before 'numbers[i]'
if u still face problem then just see my program
C Syntax (Toggle Plain Text)
#include<stdio.h> void main() { FILE *p,*p1,*p2; int arr[]={1,2,3,4,5,6,7,8,9},i; p=fopen("Raj.txt","w"); for(i=0;i<9;i++) { printf("%d\n",arr[i]); fprintf(p,"%d\n",arr[i]); } fclose(p); p1=fopen("Raj.txt","r"); for(i=0;i<9;i++) { fscanf(p1,"%d",&arr[i]); printf("%d\n",arr[i]); } fclose(p1); p2=fopen("RajCopy.txt","w"); for(i=0;i<9;i++) { fprintf(p2,"%d\n",arr[i]); } fclose(p2); }
![]() |
Similar Threads
- The Fastest way to read a .txt File (C++)
- write to an Exel file (PHP)
- Create a link in a table read from a CSV file and pass a variable (PHP)
- Write a hex file in Delphi (Pascal and Delphi)
- reading integers from a formatted file (C++)
- how to write the textbox values into a file (JSP)
- Error trying to write to existing file (C)
Other Threads in the C Forum
- Previous Thread: Binary Search of an element in an array of elements
- Next Thread: Check text file updation through C prog
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks binarysearch calculate changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fflush fgets file fork forloop framework function getlasterror givemetehcodez grade gtkwinlinux hacking hardware histogram inches include incrementoperators input intmain() iso kernel keyboard km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming standard string systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






