Not able to write the values in the file

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 3
Reputation: dhiliptce is an unknown quantity at this point 
Solved Threads: 0
dhiliptce dhiliptce is offline Offline
Newbie Poster

Not able to write the values in the file

 
0
  #1
Jan 30th, 2009
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<iostream.h>
  4. main()
  5. {
  6. FILE *fp,*fp1;
  7. int numbers[30];
  8. char *buffer;
  9.  
  10. int i=0,j;
  11. // To read the file
  12. fp=fopen("PR1.txt","r");
  13. if(fp==NULL){
  14. printf("Error: can't access file.c.\n");
  15. exit(1);
  16. }
  17. else {
  18. printf("File opened successfully.\n");
  19. for(i=0;i<10;i++)
  20. {
  21. // while(!feof(fp)) {
  22. /* loop through and store the numbers into the array */
  23. printf("Number- %d", i);
  24. fscanf(fp,"%d",&numbers[i]);
  25.  
  26.  
  27. // i++;
  28. }
  29. }
  30. printf("Number of numbers read: %d\n\n", i);
  31. printf("The numbers are:\n");
  32.  
  33. for(j=0 ; j<i ; j++) /* now print them out 1 by 1 */
  34. {
  35. printf("%d\n", numbers[j]);
  36. }
  37.  
  38. fclose(fp);
  39.  
  40. // To write the values to another file
  41. fp1= fopen("PR2.txt","w");
  42.  
  43. if(fp1=NULL) {
  44. printf("Error: can't access file.c.\n");
  45. exit(1);
  46. }
  47. else
  48. {
  49. printf("File opened successfully.\n") ;
  50. for(i=0;i<10;i++)
  51. {
  52. fprintf(fp1,"%d\n",&numbers[i]);
  53. // fwrite(&numbers[i],sizeof(numbers[i]),10,fp1);
  54.  
  55.  
  56. }
  57. }
  58. fclose(fp1);
  59. cin.get();
  60. }
Last edited by Narue; Jan 30th, 2009 at 11:05 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 4
Reputation: mad93 is an unknown quantity at this point 
Solved Threads: 1
mad93 mad93 is offline Offline
Newbie Poster

Re: Not able to write the values in the file

 
0
  #2
Jan 30th, 2009
if(fp1=NULL)

You're setting fp1 to NULL, it should be:

if(fp1==NULL)
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Not able to write the values in the file

 
0
  #3
Jan 30th, 2009
Where's your question?
Furthermore, this is a forum for the C language, and you are showing us an aberration of C++.

Use proper code tags when posting source code. Read the rules or you'll be ignored.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: dhiliptce is an unknown quantity at this point 
Solved Threads: 0
dhiliptce dhiliptce is offline Offline
Newbie Poster

Re: Not able to write the values in the file

 
0
  #4
Jan 31st, 2009
O.K... Sorry for not being clear... I will read the rules.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: dhiliptce is an unknown quantity at this point 
Solved Threads: 0
dhiliptce dhiliptce is offline Offline
Newbie Poster

Re: Not able to write the values in the file

 
0
  #5
Jan 31st, 2009
Thanks for alerting me...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 13
Reputation: rajenpandit is an unknown quantity at this point 
Solved Threads: 1
rajenpandit rajenpandit is offline Offline
Newbie Poster

Re: Not able to write the values in the file

 
0
  #6
Feb 2nd, 2009
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
  1. #include<stdio.h>
  2.  
  3. void main()
  4. {
  5. FILE *p,*p1,*p2;
  6. int arr[]={1,2,3,4,5,6,7,8,9},i;
  7. p=fopen("Raj.txt","w");
  8. for(i=0;i<9;i++)
  9. {
  10. printf("%d\n",arr[i]);
  11. fprintf(p,"%d\n",arr[i]);
  12. }
  13. fclose(p);
  14. p1=fopen("Raj.txt","r");
  15. for(i=0;i<9;i++)
  16. {
  17. fscanf(p1,"%d",&arr[i]);
  18. printf("%d\n",arr[i]);
  19. }
  20. fclose(p1);
  21. p2=fopen("RajCopy.txt","w");
  22. for(i=0;i<9;i++)
  23. {
  24. fprintf(p2,"%d\n",arr[i]);
  25. }
  26. fclose(p2);
  27. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC