943,724 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3472
  • C RSS
Nov 27th, 2007
0

Read from a file then placing strings into a 2d array

Expand Post »
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define SIZE 5
  6. #define MAX 10
  7.  
  8. void fillArray(char arr[][MAX]);
  9. void printArray(char arr[][MAX]);
  10. int fileOpen(void);
  11.  
  12. int main(void)
  13. {
  14. char arr[SIZE][MAX];
  15. int success;
  16. fileOpen();
  17. if (success == 1)
  18. {
  19. fillArray(arr);
  20. printArray(arr);
  21. }
  22. else
  23. printf("File access failed");
  24. return 0;
  25. }
  26. int fileOpen(void) // ** this were i am having the most trouble
  27. {
  28. char fileInput;
  29. char word[SIZE][MAX];
  30. FILE *input;
  31. int success;
  32. if ((input = fopen("infile", "r")) !=NULL)
  33. {
  34.  
  35. while ((fileInput = fgetc(input)) != EOF)
  36. {
  37. printf("\n file opened!");//remove
  38.  
  39. printf("\n Its working!\n ");//emove
  40. strcpy(input, word);
  41.  
  42. putchar(fileInput);
  43. fillArray(word);
  44. }
  45.  
  46.  
  47. printf("Failure in opening \"infile\" for writing.\n");
  48. fclose(input);
  49. }
  50. else
  51. {
  52. printf("\n Could not access \"infile\" for reading.\n\n");
  53. success = 0;
  54. }
  55. return success;
  56. }
  57.  
  58. void fillArray(char word[][MAX])
  59. {
  60. char arr[SIZE][MAX]; // i also need guidance on how to fill the array with the
  61. info from a the file "infile"
  62. int i, j;
  63. int names;
  64. names = 0;
  65. printf("FIllARRAY TIME!");
  66. for (i = 0; i < SIZE; i++)
  67. fscanf("%s", word[i]);
  68. for(j = 0; j < MAX; j++)
  69. if ( i < SIZE)
  70. if ( j < MAX)
  71. {
  72. printf("%s", i, word[i][MAX]);
  73. names = i;
  74. printf("Named pairs = %d", names);
  75. printf("\n\n");
  76. }
  77. else
  78. printf("problem with data presented!");
  79.  
  80. }
  81.  
  82.  
  83. void printArray(char arr[][MAX])
  84. {
  85. int i, j;
  86.  
  87. for (i = 0; i < SIZE; i++)
  88. {
  89. for (j = 0; j < MAX; j++)
  90. printf("%s", arr[i][j]);
  91. printf("\n");
  92. }
  93.  
  94. }

any in sight would be helpful. im really under the gun and have been trying to get this to work for a couple of days.

thanks in advance
David
Last edited by WaltP; Nov 27th, 2007 at 5:56 am. Reason: Use the PREVIEW button!!!! Fixed CODE tags
Reputation Points: 31
Solved Threads: 0
Newbie Poster
controlsi is offline Offline
7 posts
since Nov 2007
Nov 27th, 2007
0

Re: Read from a file then placing strings into a 2d array

Click to Expand / Collapse  Quote originally posted by controlsi ...
any in sight would be helpful. im really under the gun and have been trying to get this to work for a couple of days.

thanks in advance
David
With what? You didn't mention anything was wrong. Did you read the post titled
Read Me: Read This Before Posting?
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Nov 27th, 2007
0

Re: Read from a file then placing strings into a 2d array

sorry about that and thank you for formatting my code. I will make sure to do that from now on and i took your advice and read the post mentioned above.

Well, my problem is that the code above is suppose to read strings from a file and then take those strings and place them into a 2d array.

The problem that i am having is reading the actual strings from the text file and placing them into the array. It only seems to be printing the first letter in the file then nothing else.

input:
infile(which is the txt file i am using) contains:
lastname, firstname (about 5 names)
and the names are not sorted

output:
the names are printed here exactly as they are shown in the text file "infile"
ex.
lastname, firstname
doe, john
francis, chris

any help would be very appreciated
Last edited by controlsi; Nov 27th, 2007 at 10:15 am.
Reputation Points: 31
Solved Threads: 0
Newbie Poster
controlsi is offline Offline
7 posts
since Nov 2007
Nov 27th, 2007
0

Re: Read from a file then placing strings into a 2d array

Try commenting your code. That would tell you quite a lot, if you comment exactly.

Based on your code:
  1. if ((input = fopen("infile", "r")) !=NULL) // open the file
  2. {
  3.  
  4. while ((fileInput = fgetc(input)) != EOF) // read a single character
  5. {
  6. strcpy(input, word); // copy WORD into INPUT
  7.  
  8. putchar(fileInput); // Output the character read
  9. fillArray(word);
  10. }
You never did anything with the single character you read. And you never read any more than that.
Isn't INPUT a file designator? Does strcpy() work on such an item? Look it up. I know your compiler complained about that.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006

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: executed file does not return 0
Next Thread in C Forum Timeline: fgets crash issues





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


Follow us on Twitter


© 2011 DaniWeb® LLC