| | |
Help reading a text file of any size
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 5
Reputation:
Solved Threads: 0
Hi did what was suggested with the fopen/fclose. Could someone now help to me prevent the same line in a text file from being used twice? Thanks
C Syntax (Toggle Plain Text)
int initialize_array(char phrase[], char puzzle[], char clue[]){ FILE* phraseFile; /*input file*/ char temp[100]; /*temporary array*/ int line; /*line from which word and clue are chosen*/ int i=0; /*counter for loop*/ int len; /*length of line from file*/ int count=0; phraseFile = fopen("clues.txt", "r"); /*opens file*/ if (!phraseFile) { fprintf(stderr, "Oops - file not opened !\n"); /*if there is error in opening file program is exited*/ exit(1); } while(fgets(temp,100,phraseFile)!=0){ ++count; } fclose(phraseFile); fopen("clues.txt", "r"); line= rand()%count; while(line--){ fgets(temp, 100, phraseFile); /*gets line and stores in temp*/ } len=strlen(temp); if(temp[len-1]=='\n') temp[len-1]=0; /*places the null terminating character after word*/ /*separates clues from word*/ strcpy(clue,strtok(temp, "%")); /* strcpy(phrase,strtok(NULL,"%"));*/ char tmp[WORD_LENGTH]; strcpy(tmp,strtok(NULL,"%")); strcpy(phrase, tmp + 1); strcpy(puzzle,phrase); /*strcpy(puzzle,phrase);*/ while(i<strlen(puzzle)){ if(isalpha(puzzle[i])){ puzzle[i] = '*'; /*hides word*/ } ++i; } fclose(phraseFile); /*closes file*/ return i; }
in "main", declare an integer array of size equal to or greater than the number of possible lines, initializing all elements to zero on startup.
each time you go to use a line, check to see that the array element at that line index is still zero. if not, choose a different line.
once you use a line from the file, set the value of the array element at that line's index number to one.
each time you go to use a line, check to see that the array element at that line index is still zero. if not, choose a different line.
once you use a line from the file, set the value of the array element at that line's index number to one.
Last edited by jephthah; May 4th, 2008 at 2:27 am.
![]() |
Similar Threads
- Read text file contents per line (C++)
- c++ reading text file (C++)
- C++ Reading from a text file (C++)
- C: - reading in from text (C++)
- file size (C++)
- Reading an array from a file (Java)
- reading a file into code (Java)
- Stack Queue Fstream (C++)
Other Threads in the C Forum
- Previous Thread: finding factorial of a number.
- Next Thread: how to reverse a string
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures suggestions system systemcall test testautomation unix user voidmain() wab win32api windows.h






