944,093 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 373
  • C RSS
Oct 26th, 2009
0

file opening problem!!!!!

Expand Post »
hi!

i'm trying to write the code to open the file and read the contents and write to another file with uppercase but the problem i'm having is path problem everything seems pretty fine but it says Steam != NULL , as long as i know its could'nt locating the file.

NB: i'm using Visual C++ 2008 as IDE.

Please have a look at it and correct me if i'm wrong.
Br.
  1.  
  2. #include "stdafx.h"
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6.  
  7. int main(void)
  8. {
  9.  
  10. int ch;
  11.  
  12. int NumAlpha = 0, NumDigit = 0, NumLower = 0, NumUpper = 0, Count = 0, Place = 0;
  13. char *my_word, *my_word2, *my_word3;
  14.  
  15. my_word = "dog";
  16. my_word2 = "cat";
  17. my_word3 = "rat";
  18.  
  19. long int pos;
  20. const char readerfilepath[500] = "D:\\TEMP\\readObject.txt";
  21. const char writerfilepath[500] = "D:\\TEMP\\writeObject.txt";
  22.  
  23.  
  24. FILE *ipfileptr;
  25. FILE *opfileptr;
  26.  
  27. ipfileptr = fopen(readerfilepath, "r");
  28. if (ipfileptr == NULL )
  29. {
  30. printf("Reader File not opened!\n");
  31. exit(8);
  32. }
  33. else
  34. {
  35. printf("Reader File opened successfuly!\n");
  36. }
  37.  
  38. opfileptr = fopen(writerfilepath, "w+");
  39.  
  40. if (ipfileptr == NULL )
  41. {
  42. printf("Writer File not opened!\n");
  43. exit(8);
  44. }
  45. else
  46. {
  47. printf("Writer File opened successfuly!\n");
  48. }
  49. while ((ch = getc(ipfileptr)) != EOF){
  50.  
  51. if (isalpha(ch))
  52. NumAlpha++; /* add 1 to NumAlpha count */
  53.  
  54. if (islower(ch))
  55. NumLower++; /* add 1 to NumLower count */
  56.  
  57. else
  58. if (isupper(ch))
  59. NumUpper++; /*add 1 to NumAlpha count */
  60.  
  61. else
  62.  
  63. if (isdigit(ch))
  64. NumDigit++; /*add 1 to digit count*/
  65.  
  66.  
  67. //ch = putc(toupper(ch),opfileptr);
  68.  
  69. if (ch == *my_word ||*my_word2 || *my_word3){
  70. Count++;
  71.  
  72. if((pos = ftell(ipfileptr)) != EOF){
  73. Place++;
  74. }
  75.  
  76.  
  77. }
  78. ch = putc(toupper(ch),opfileptr);
  79.  
  80.  
  81. }
  82.  
  83. printf("\nNumAlpha = %d NumDigit = %d NumLower = %d NumUpper = %d\nCount = %d\nPosition = %d\n", NumAlpha, NumDigit, NumLower, NumUpper ,Count,Place);
  84. // getchar();
  85.  
  86.  
  87. fclose(ipfileptr);
  88. fclose(opfileptr);
  89.  
  90.  
  91. return 0;
  92. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
j_cart007 is offline Offline
17 posts
since May 2009
Oct 26th, 2009
1
Re: file opening problem!!!!!
you can't do this:
  1. if (ch == *my_word ||*my_word2 || *my_word3)

you have to specify what you're testing for each OR'ed condition.
  1. if (ch == *my_word || ch == *my_word2 || ch == *my_word3)
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Oct 27th, 2009
1
Re: file opening problem!!!!!
Also you are not writing C++ code so all your variable definitions need to be at the top of your functions, before any executable statements.

And for us to follow your code, use proper formatting
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Oct 27th, 2009
0
Re: file opening problem!!!!!
can u have at it again pls and sorry about any mistake again.
Br.

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4.  
  5. int main(void)
  6. {
  7.  
  8. int ch;
  9. long int pos;
  10. int NumAlpha = 0, NumDigit = 0, NumLower = 0, NumUpper = 0;
  11. int Count = 0, Place = 0;
  12.  
  13. char *my_word, *my_word2, *my_word3;
  14.  
  15. const char readerfilepath[500] = "D:\\TEMP\\readObject.txt";
  16. const char writerfilepath[500] = "D:\\TEMP\\writeObject.txt";
  17.  
  18. my_word = "dog";
  19. my_word2 = "cat";
  20. my_word3 = "rat";
  21.  
  22. FILE *ipfileptr;
  23. FILE *opfileptr;
  24.  
  25. ipfileptr = fopen(readerfilepath, "r");
  26. if (ipfileptr == NULL )
  27. {
  28. printf("Reader File not opened!\n");
  29. exit(8);
  30. }
  31. else
  32. {
  33. printf("Reader File opened successfuly!\n");
  34. }
  35.  
  36. opfileptr = fopen(writerfilepath, "w+");
  37.  
  38. if (ipfileptr == NULL )
  39. {
  40. printf("Writer File not opened!\n");
  41. exit(8);
  42. }
  43. else
  44. {
  45. printf("Writer File opened successfuly!\n");
  46. }
  47.  
  48.  
  49. while ((ch = getc(ipfileptr)) != EOF){
  50.  
  51. if (isalpha(ch))
  52. NumAlpha++; /* add 1 to NumAlpha count */
  53.  
  54. if (islower(ch))
  55. NumLower++; /* add 1 to NumLower count */
  56. else
  57. if (isupper(ch))
  58. NumUpper++; /*add 1 to NumAlpha count */
  59.  
  60. else
  61.  
  62. if (isdigit(ch))
  63. NumDigit++; /*add 1 to digit count*/
  64.  
  65.  
  66.  
  67.  
  68. if (ch == *my_word ||ch == *my_word2 || ch == *my_word3)
  69. {
  70. Count++;
  71.  
  72. if((pos = ftell(ipfileptr)) != EOF)
  73. {
  74. Place++;
  75. }
  76.  
  77.  
  78. }
  79.  
  80. ch = putc(toupper(ch),opfileptr);
  81.  
  82.  
  83. }
  84.  
  85. printf("\nNumAlpha = %d NumDigit = %d NumLower = %d NumUpper = %d\nCount = %d\nPosition = %d\n", NumAlpha, NumDigit, NumLower, NumUpper ,Count,Place);
  86.  
  87. fclose(ipfileptr);
  88. fclose(opfileptr);
  89.  
  90.  
  91. return 0;
  92. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
j_cart007 is offline Offline
17 posts
since May 2009

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: Read Me : Always mark the thread solved when the problem is solved
Next Thread in C Forum Timeline: Microsoft Visual studio Runtime





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


Follow us on Twitter


© 2011 DaniWeb® LLC