943,840 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 537
  • C RSS
Jan 30th, 2009
0

Not able to write the values in the file

Expand Post »
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dhiliptce is offline Offline
3 posts
since Jan 2009
Jan 30th, 2009
0

Re: Not able to write the values in the file

if(fp1=NULL)

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

if(fp1==NULL)
Reputation Points: 10
Solved Threads: 1
Newbie Poster
mad93 is offline Offline
4 posts
since Jan 2009
Jan 30th, 2009
0

Re: Not able to write the values in the file

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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 31st, 2009
0

Re: Not able to write the values in the file

O.K... Sorry for not being clear... I will read the rules.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dhiliptce is offline Offline
3 posts
since Jan 2009
Jan 31st, 2009
0

Re: Not able to write the values in the file

Thanks for alerting me...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dhiliptce is offline Offline
3 posts
since Jan 2009
Feb 2nd, 2009
0

Re: Not able to write the values in the file

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. }
Reputation Points: 9
Solved Threads: 1
Newbie Poster
rajenpandit is offline Offline
13 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Binary Search of an element in an array of elements
Next Thread in C Forum Timeline: Check text file updation through C prog





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC