file i/o problem!

This is a discussion on file i/o problem! within the C++ Programming forums, part of the General Programming Boards category; Why doesn't this work? Any help would be appreciated! Code: #include <iostream> #include <fstream> using namespace std; int main() { ...

  1. #1
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369

    file i/o problem!

    Why doesn't this work? Any help would be appreciated!

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	char *test = "blah";
    
    	ofstream fout;
    	ifstream fin;
    
    	fout.open("test.txt");
    	fout << test;
    	fout.close();
    
    	test = "bleh";			// blah to bleh
    
    	fin.open("test.txt");
    	fin >> *test;			// bleh to blah
    	fin.close();
    
    	cout << test << endl;	// bleh shows up, which isn't what I want
    
    	return 0;
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This is a fixed version.

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	char test[] = "blah";
    
    	ofstream fout;
    	ifstream fin;
    
    	fout.open("test.txt");
    	fout << test;
    	fout.close();
    
    	strcpy(test, "bleh");			// blah to bleh
    
    	fin.open("test.txt");
    	fin >> test;			// bleh to blah
    	fin.close();
    
    	cout << test << endl;	
    
    	return 0;
    }
    In your code, the test variable is a pointer which points to a string literal, so you cannot modify the text. I made mine an array, which makes things work a little better.

    Also, this:
    cin >> *test
    inputs one character only, and dereferencing test gets you a char.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Ahh thanks, that works!
    Last edited by abrege; 02-14-2003 at 06:08 PM.
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  4. #4
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Ohhh and yet another question. How would I go about picking up whitespace with fin?
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    fin.getline() gets you a complete line. Just make sure your array is big enough to receive whatever is being read into it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    This is probably my last question for a while Is there any way to fix this:

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	int five;
    	char test[] = "two words";
    
    	ofstream fout;
    	ifstream fin;
    
    	fout.open("test.txt");
    	fout << 5 << endl << test;
    	fout.close();
    
    	strcpy(test, "oneword");
    
    	fin.open("test.txt");
    	fin >> five;
    	fin.getline(test, 120, '\n');
    	fin.close();
    
    	cout << test << endl;
    
    	return 0;
    }
    test comes up as blank when I try to get an int before it
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  7. #7
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    strcpy(test, "oneword"); ?

    My guess would be that you're picking up the new line between 5 and "two words" and storing that in test.

    Check that your files have been opened before using them.

    Code:
    fin.open("test.txt", ios::in);
    	
    if(fin.fail())
    {
      cout << "failed to open file.";
      return -1;
    }
    	
      fin >> five;
    	
      while(fin.peek() != EOF) or while(fin)
        fin.getline(test, sizeof(test), '\n');
    		
      fin.close();
    
      cout << five << endl;
      cout << test << endl;
    Try that and see if it works.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  8. #8
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Hmmm I'm still getting some garbage values here's the actually code I'm using:

    Code:
    void PLAYER::saveFile()
    {
    	ofstream fout;
    	fout.open(name);
    
    	fout << exp << endl;
    	fout << gold << endl;
    	fout << health << endl;
    	fout << level << endl;
    	fout << maxExp << endl;
    	fout << maxHealth << endl;
    
    	for(int i = 0; i < 5; i ++)
    	{
    		fout << inventory.slot[i].name << endl;
    		fout << inventory.slot[i].price << endl;
    		fout << inventory.slot[i].value << endl;
    	}
    	
    	fout << weapon.name << endl;
    	fout << weapon.price << endl;
    	fout << weapon.value << endl;
    
    	fout.close();
    }
    
    bool PLAYER::loadFile(char *n)
    {
    	ifstream fin;
    	fin.open(n, ios::in);
    
    	if(fin.fail())
    		return false;
    	else
    	{
    		name = n;
    		fin >> exp;     // all these are ints
    		fin >> gold;
    		fin >> health;
    		fin >> level;
    		fin >> maxExp;
    		fin >> maxHealth;
    
    		for(int i = 0; i < 5; i ++)
    		{
    			while(fin.peek() != EOF)
    				fin.getline(inventory.slot[i].name, sizeof(inventory.slot[i].name), '\n');
    
    			fin >> inventory.slot[i].price;
    			fin >> inventory.slot[i].value;
    		}
    
    		while(fin.peek() != EOF)
    			fin.getline(weapon.name, sizeof(weapon.name), '\n');
    
    		fin >> weapon.price;
    		fin >> weapon.value;
    
    		fin.close();
    	}
    
    	return true;
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Is it the read or the write you're having trouble with. I mean, if you do the write, then open the first in a text editor, does it look good?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    yeah, it looks fine, I'm pretty sure the errror is on whats being read
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  11. #11
    Unreg1
    Guest
    >>while(fin.peek() != EOF)
    So, you read until EOF, then you try and read some more?? Thats a logic problem

    If this doesn't help, post the definition of inventory and weapon, and a sample of the contents of the input file.

  12. #12
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    item:
    Code:
    #define empty "-"
    
    class item
    {
    public:
    	char name[100];
    	int price, value;
    
    	item(char *n, int p, int v);
    	item();
    	~item();
    
    	item &operator=(const item & cpy);
    	bool operator==(const item & cmp);
    	bool operator!=(const item & cmp);
    
    	bool isEmpty();
    	void removeItem();
    };
    
    item::item(char *n, int p, int v)
    {
    	strcpy(name, n);
    	price = p;
    	value = v;
    }
    
    item::item()
    {
    	*this = item(empty, 0, 0);
    }
    
    item::~item()
    {}
    
    item &item::operator =(const item & cpy)
    {
    	strcpy(name, cpy.name);
    	price = cpy.price;
    	value = cpy.value;
    
    	return *this;
    }
    
    bool item::operator ==(const item & cmp)
    {
    	if(!strcmp(name, cmp.name) && price == cmp.price && value == cmp.value)
    		return true;
    
    	return false;
    }
    
    bool item::operator !=(const item & cmp)
    {
    	return *this == cmp ? false : true;
    }
    
    bool item::isEmpty()
    {
    	return *this == item(empty, 0, 0) ? true : false;
    }
    
    void item::removeItem()
    {
    	*this = item(empty, 0, 0);
    }
    file: (looks healthy)
    Code:
    0
    15
    10
    1
    20
    10
    Health Potion
    5
    5
    -
    0
    0
    -
    0
    0
    -
    0
    0
    -
    0
    0
    Weapon is declared as an item, and the inventory is an array of five items. Hope that helps! :D
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File i/o problem
    By tezcatlipooca in forum C++ Programming
    Replies: 18
    Last Post: 01-01-2007, 09:01 AM
  2. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 04:32 PM
  3. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 01:14 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21