I want to read from a 2d array which has strings in it. The thing is that the file seems to get created but when i am trying to read from the array has nothing stored in the file. The file is empty even after the write function. Please let me know where i am going wrong.
[insert]Code:#include <stdlib.h> #include <stdio.h> #include <string.h> #include <time.h> #define MAX_WORD_LENGTH 31 #define MAX_WORDS 50 /* Function prototypes */ int readFile(char* inputFileName, int* wordCount, char array[][MAX_WORD_LENGTH], int flag); //int qsort(char array[][MAX_WORD_LENGTH], int* wordCount); int writeToFile(char* outFileName, int wordCount, char array[][MAX_WORD_LENGTH]); //void printTime(time_t begin); void usage(char* processName); // enter any other function prototype you may wish int main(int argc, char* argv[]) { char array[MAX_WORDS][MAX_WORD_LENGTH]; int wordCount = 0; switch (argc) { case 3: readFile(argv[1], &wordCount, array,0); writeToFile(argv[2], wordCount, array); break; case 4: { if (strcmp(argv[1], "-s") == 0) { readFile(argv[2], &wordCount, array,1); writeToFile(argv[3], wordCount, array); } else if (strcmp(argv[1], "-t") == 0) { readFile(argv[2], &wordCount, array, 2); writeToFile(argv[3], wordCount, array); } else { usage(argv[0]); } break; } case 5: default: usage(argv[0]); break; } return 0; } // Define all your functions here int readFile(char* inputFileName, int* wordCount, char array[][MAX_WORD_LENGTH], int flag) { FILE *fp; fp = fopen(inputFileName, "r"); if(fp == NULL) { printf("\n Unable to open the file. Aborting"); exit(EXIT_FAILURE); } while(fscanf(fp, "%s", array[(*wordCount)++]) > 0); return 0; } int writeToFile(char* outFileName, int wordCount, char array[][MAX_WORD_LENGTH]) { int i = 0; FILE *fp; fp = fopen(outFileName, "w"); if(fp == NULL) { printf("\n Unable to open the file. Aborting"); exit(EXIT_FAILURE); } for(i = 0; i< wordCount; i++) { fprintf(fp, "%s\n", array[i++]); } return 0; } void usage(char* processName) { printf("Usage %s [-s] [-t] inputFileName outputFileName\n", processName); }



LinkBack URL
About LinkBacks



