| | |
Read from a file then placing strings into a 2d array
![]() |
•
•
Join Date: Nov 2007
Posts: 7
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #define SIZE 5 #define MAX 10 void fillArray(char arr[][MAX]); void printArray(char arr[][MAX]); int fileOpen(void); int main(void) { char arr[SIZE][MAX]; int success; fileOpen(); if (success == 1) { fillArray(arr); printArray(arr); } else printf("File access failed"); return 0; } int fileOpen(void) // ** this were i am having the most trouble { char fileInput; char word[SIZE][MAX]; FILE *input; int success; if ((input = fopen("infile", "r")) !=NULL) { while ((fileInput = fgetc(input)) != EOF) { printf("\n file opened!");//remove printf("\n Its working!\n ");//emove strcpy(input, word); putchar(fileInput); fillArray(word); } printf("Failure in opening \"infile\" for writing.\n"); fclose(input); } else { printf("\n Could not access \"infile\" for reading.\n\n"); success = 0; } return success; } void fillArray(char word[][MAX]) { char arr[SIZE][MAX]; // i also need guidance on how to fill the array with the info from a the file "infile" int i, j; int names; names = 0; printf("FIllARRAY TIME!"); for (i = 0; i < SIZE; i++) fscanf("%s", word[i]); for(j = 0; j < MAX; j++) if ( i < SIZE) if ( j < MAX) { printf("%s", i, word[i][MAX]); names = i; printf("Named pairs = %d", names); printf("\n\n"); } else printf("problem with data presented!"); } void printArray(char arr[][MAX]) { int i, j; for (i = 0; i < SIZE; i++) { for (j = 0; j < MAX; j++) printf("%s", arr[i][j]); printf("\n"); } }
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
•
•
•
•
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
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Nov 2007
Posts: 7
Reputation:
Solved Threads: 0
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
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.
Try commenting your code. That would tell you quite a lot, if you comment exactly.
Based on your code:
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
Based on your code:
C Syntax (Toggle Plain Text)
if ((input = fopen("infile", "r")) !=NULL) // open the file { while ((fileInput = fgetc(input)) != EOF) // read a single character { strcpy(input, word); // copy WORD into INPUT putchar(fileInput); // Output the character read fillArray(word); }
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Reading in a *.csv file and loading the data into an Array (Java)
- save file names into char array (C)
- C++ How can read from file.txt & where can save this file(file.txt) to start reading (C++)
- Read file with unknown buffer length (C)
- Still need help with opening a file and store the data on it in an array (C++)
- CSV file (C)
- Read in a file and store in char array (C)
Other Threads in the C Forum
- Previous Thread: executed file does not return 0
- Next Thread: fgets crash issues
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






