file opening problem!!!!!

Reply

Join Date: May 2009
Posts: 7
Reputation: j_cart007 is an unknown quantity at this point 
Solved Threads: 0
j_cart007 j_cart007 is offline Offline
Newbie Poster

file opening problem!!!!!

 
0
  #1
32 Days Ago
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. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso
 
1
  #2
32 Days Ago
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)
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei
 
1
  #3
31 Days Ago
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
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 7
Reputation: j_cart007 is an unknown quantity at this point 
Solved Threads: 0
j_cart007 j_cart007 is offline Offline
Newbie Poster
 
0
  #4
30 Days Ago
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. }
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC