GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Jan-2004, 01:54
bengaltgrs bengaltgrs is offline
New Member
 
Join Date: Jan 2004
Posts: 5
bengaltgrs is on a distinguished road

reading a .txt


I was wondering if there is a way to read information from a .txt and place it into the code just as if it had been written there to begin with, short of just using #include "new.txt". I've looked through the ifstream uses but can't seem to figure this out.
  #2  
Old 19-Jan-2004, 07:14
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
What exactly will be in the .txt file? Remember that once you've your program compiled, you cannot add bits to it. You can only get the program to open files, and read their input.

GF
  #3  
Old 19-Jan-2004, 14:50
bengaltgrs bengaltgrs is offline
New Member
 
Join Date: Jan 2004
Posts: 5
bengaltgrs is on a distinguished road
The contents of the file is basically,

Width = 800;
Height = 600;

It's set up right now so that when you choose the resolution, it writes what you choose into a file. I was wondering if there was a way to read from the file, even though it has changes, and without recompiling.
  #4  
Old 19-Jan-2004, 16:19
bengaltgrs bengaltgrs is offline
New Member
 
Join Date: Jan 2004
Posts: 5
bengaltgrs is on a distinguished road
I just realized that if I knew how to just change the window resolution, it would work better than writing it to a file. Unfortuantely I'm still pretty new at all this. It's a DirectX 9.0 program if that helps you at all.

Thanks for the help
  #5  
Old 20-Jan-2004, 16:20
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
bengaltgrs.

It appears that you were on the wrong track. It sounds like you wanted a program to read the resolution from a text file at run time. A compiler directive, ie #include, is just that. It directs the compiler what to do and has nothing to do with run-time.
  #6  
Old 20-Jan-2004, 17:27
bengaltgrs bengaltgrs is offline
New Member
 
Join Date: Jan 2004
Posts: 5
bengaltgrs is on a distinguished road
I've tried using #include, but every time I run the program and write something new to the .txt, it tells me the the .txt is out of date and the program needs to be re-compiled.
  #7  
Old 20-Jan-2004, 18:45
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by bengaltgrs
I've tried using #include, but every time I run the program and write something new to the .txt, it tells me the the .txt is out of date and the program needs to be re-compiled.

Exactly. When a program is compiled, the contents of the #include file is made a part of the compiled file. Therefore, whatever was in that file at compile time is what will be in your program. And only by recompiling it will the new text file be a part of your program.

It really sounds like you want a configuration type file that is read in at run-time. If this is what you are after, I've got some really basic code that will read/write a configuration file.
  #8  
Old 20-Jan-2004, 20:06
bengaltgrs bengaltgrs is offline
New Member
 
Join Date: Jan 2004
Posts: 5
bengaltgrs is on a distinguished road
That sounds exactly like what I need, if I could get that from you I think it would help a lot. Thanks for your help.
  #9  
Old 21-Jan-2004, 09:04
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Bengaltgrs:

I have hacked together some of the stuff that I have used in the past. You will need to test this yourself as I did this on the fly. It should work, but no guarantees. If you are going to use this, you should really add some error checking. It is rather rudimentary.

Also, this is may seem kind of long, but most of it could be put in a library to be reused and then only the few lines at the bottom would need to be used.

There are other ways of doing this, but they involve more coding, etc. Anyone that has any comments or suggestions, they are definitely welcome. Also, if anyone makes this more robust and usable, I would like it if they sent it back to me.

Alrighty then. First of all, this needs a small list routine that I wrote. This is all inline, so I just put this in a .h file and there is no library to compile:

list1.h
CPP / C++ / C Code:
#ifndef __LIST_H
#define __LIST_H

class list{
	struct data{
		void* info;
		data* next;
	};

	data*	head;
	data*	current;

public:
	list(){
		head = current = 0;
     }

	~list(){
		current = head;
		while(!is_empty())
			rem_data();
     }


	void add_data(void* new_data){

		data* temp;

     	temp = new data;
		temp->next = 0;
		temp->info = new_data;
		if(!is_empty()){
			end();
			current->next = temp;
		}
        else{
			head = temp;
		}
		current = temp;
	};

	void add_atpos(void* new_data){
		data* temp = new data;
		temp->info = new_data;
		if(is_empty()){
			temp->info = new_data;
			temp->next = 0;
			head = temp;
			current = head;
		}
		else{
			temp->info = current->info;
			temp->next = current->next;
			current->info = new_data;
			current->next = temp;
		}
	};

	void rem_data(){

		data* temp;

		if(!is_empty()){
			if(is_at_home())
				head = head->next;
              else{
			  	temp = head;
				while(temp->next != current)
					temp = temp->next;
                   temp->next = current->next;
			}
			delete current;
			home();
		}
     };

	void* get_data(){
		if( (!is_empty()) && (is_valid()) )
			return(current->info);
   		else
			return 0;
     };

	void home(){
		current = head;
	};

	char is_at_home(){
		if(!is_empty())
			if(current == head)
				return 1;
         	return 0;
	};

	char is_at_end(){
		if(!is_empty()){
			if(!current)
				return 1;
			if(!current->next)
				return 1;
		}
    	return 0;
	};

    	char is_empty(){
		if(head)
			return 0;
         return 1;
     };

	char is_valid(){
		if(current)
			return 1;
   		return 0;
	};

	void end(){
		home();
		if(!is_empty())
			while(next());
		else
		  	current = 0;
     };

	int next(){
		if(!is_empty())
			if(current->next){
				current = current->next;
				return 1;
			}
		return 0;
	};
};

#endif

Then a structure to hold configuration items is needed
Configuration Structure
CPP / C++ / C Code:
struct config_entry{
    char *name;
    char *val;
};


Next, there a few routines that are needed to read, write & parse a configuration file.
Parse a configuration list
CPP / C++ / C Code:
char* find_value(list* parms, char* name)
{
	char*			value;
	config_entry*	data;

	if(parms->is_empty())
		return NULL;
	parms->home();
	do{
		data = (config_entry*) parms->get_data();
		if(!strcmp(data->name,name)){
			value = (char*) malloc(strlen(data->val)+1);
			strcpy(value,data->val);
			return value;
		}
	}while(parms->next());
	return NULL;
}

Read a configuration file
CPP / C++ / C Code:
list* read_config(FILE* fp){

	list*	parms = new list;
	char*	line = new char[200];
	config_entry*	new_item;
	char 	*parse,
			*parse2;
	int		space;

	do{
		line = fgets(line,199,fp);				//Read a single line from file
        parse = line;
		if(!feof(fp){
			parse = line;
			if( (*parse!=";") && (*parse!='\n') ){		//Allow for blank lines and comment lines
				new_item = new config_entry;
				space = 0;
				while((*parse != '=') && (*parse != 0)){
      				if(*parse == ' ')
         				space++;
         			else
         				space = 0;
        		 	parse++;
     			}
      			*(parse - space) = 0;
				new_item->name = new char[strlen(line)+1];
				strcpy(new_item->name,line);
				space = 0;
				parse++;
				parse2 = parse;
				while((*parse != '\n') && (*parse != 0)){
					if(*parse == ' ')
						space++;
					else
						space = 0;
					parse++;
				}
				*(parse - space) = 0;
				new_item->value = new char[strlen(parse2)+1];
				strcpy(new_item->value,parse2);
				parms->add_item((void*) new_item);
			}
		}
		else
			break;
	}while(1);

	return parms;
}


Write a configuration file
CPP / C++ / C Code:
void wvar(FILE* fp,list* parms){

	char* 			line = new char[200];
	config_entry*	item;

    if(!parms->is_empty()){
		parms->home();
		do{
			item = (config_entry*) parms->get_data();
			strcpy(line,item->name);
			strcat(line,"=");
			strcat(line,item->value);
			fputs(string,fp);
			fputc('\n',fp);
		}while(parms->next());
	}
}

That is the basic library, now this is the custom code that you would write to use this...

Your code to read a configuration file:
CPP / C++ / C Code:
	FILE* fp = fopen("file.cfg","r");
	list* parms = read_config(fp);
	fclose(fp);
	width = atoi(find_value(parms,"width"));
	height = atoi(find_value(parms,"height"));


To add a configuration entry:
CPP / C++ / C Code:
	config_entry item;

	item->name = new char[20];
	item->value = new char[20];

	strcpy(item->name,"colordepth");
	strcpy(item->value,"16");
	list->add_data((void*) item);


And to write a file
CPP / C++ / C Code:
	FILE* fp = fopen("file.cfg","w");
	write_config(fp,parms);

Hopefully, this will give you a starting point. Adjust/modify as you see fit for your application.
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading from file jmenendezr C++ Forum 2 26-Feb-2006 16:00

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 12:23.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.