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

Reply

Join Date: Nov 2007
Posts: 7
Reputation: controlsi is an unknown quantity at this point 
Solved Threads: 0
controlsi controlsi is offline Offline
Newbie Poster

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

 
0
  #1
Nov 27th, 2007
  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
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

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

 
0
  #2
Nov 27th, 2007
Originally Posted by controlsi View Post
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?
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: Nov 2007
Posts: 7
Reputation: controlsi is an unknown quantity at this point 
Solved Threads: 0
controlsi controlsi is offline Offline
Newbie Poster

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

 
0
  #3
Nov 27th, 2007
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.
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

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

 
0
  #4
Nov 27th, 2007
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.
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  
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