| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Read a string from a file and execute a function
Hi,
I have a general programming question. If I have an external file in C and in my main program, I simply want to read data from the file, how do I do it so that the string it reads from a line in the file, in the main program I want to execute its fucntion. Example, string read from file is "ICP_IP" and in main I want to execute that particular function which is defined in the main. This will have to be done in my while loop in main which will stop when it hits eof. Is it a better idea to simply read the data and save it all in a structure and then do it or do each one dynamically like now? thanks any hep would be GREATLY appreciated |
|
#2
|
|||
|
|||
|
do u mean this?
=> int main() { FILE *fp; fp=fopen("input.txt","r"); char s[256]; while( fgets(s, sizeof(s),fp)!=NULL) { cout<<s<<endl; //func(s); //call the function to manipulate this line } fclose(fp); return 0; } |
|
#3
|
|||
|
|||
|
hm.. try this
struct string { string name }; ifsteam inFile; string filename; char buffer[40]; InFile.open(filename.c_str()); if(!inFile) { cout<<"cannot read file"<<endl; return 1; } int track=0; while(track<#ofnames && !inFile.eof()) { inFile.get(buffer,40); string.name=buffer; } there might be a few errors but i think i put the general idea into there |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Read a string from a file and execute a function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|