Thread: creating a variable number of variables based on data within a text file

  1. #1
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167

    creating a variable number of variables based on data within a text file

    I want to take the below code and modify it, so that it reads the text from example.txt and then creates a variable for each character in that document, so if the .txt said, "Hello" the program should do

    pos1=H;
    pos2=e'
    pos3=l;
    pos4=l;
    pos5=o;



    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <conio.h>
    
    
    int main()
    {
    
    	char str[100];
    	ifstream b_file("example.txt");
    	b_file.getline(str,100,'\n');
    	cout << str << endl;
    	b_file.close();
    	getch();
    	return 0;
    }
    AIM: MarderIII

  2. #2
    You would need to create the variables on the heap. By using the new keyword you could do this. But I am too new to help. Sorry.

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    You'll need an array, like you're using... or you can flex your C++ muscles and use an std::string

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main(int argc, char** argv)
    {
        std::ifstream fin("example.txt");
        std::string buffer;
        std::getline(fin, buffer);
        std::cout << buffer << std::endl;
    
        std::cin.get();
        return 0;
    }
    Also, opt for the new standard headers <iostream> <fstream> etc. Drop the .h

  4. #4
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    whatever this :: stuff is, Borland C++ 5.02 doesnt seem to recognize it
    AIM: MarderIII

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Do you know arrays, pointers, and strings?
    if you don't go learn them.
    If you know them, rewrite that program using them.
    Then post when you have some difficulties.
    none...

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    This might be what your looking for :

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main( int argc, char *argv[] )
    {
    
    	fstream file("test.txt", ios::in | ios::out);
    
    	file.seekp(0, ios_base::end);
    	
    	int length = file.tellg();
    		
    	file.seekp(0, ios_base::beg);
    
    	char *buf = new char[length];
    
    	char ch;
    	int counter=0;
    	while (!file.eof())
    	{
    		file.get(ch);
    		buf[counter++] = ch;
    	}
    
    	for (int x=0; x < length; x++)
    		cout << buf[x];
    
    	cout << endl;
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM