Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 3rd, 2006, 8:01 AM   #1
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 4 n00b is on a distinguished road
Send a message via MSN to n00b
work with files (fstream)

i've been trying to make a really simple program work. i want to input a few lines into a text file called text1.txt, but i get all kinds of errors after compiling this bit of a code. errors are such as 'dat' : undeclared identifier.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	fstream.dat;
	dat.open("text1.txt",ios::out);
	dat << "Please work" << endl;
	dat << "work damn you!!!!" << endl;
	int i=150;
	dat << i << endl;
	dat.close();
	return 1;
}
n00b is offline   Reply With Quote
Old Nov 3rd, 2006, 8:03 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Start here: fstream.dat;

Look at your sample code again.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 3rd, 2006, 8:05 AM   #3
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 4 n00b is on a distinguished road
Send a message via MSN to n00b
omg...yes indeed
n00b is offline   Reply With Quote
Old Nov 3rd, 2006, 8:19 AM   #4
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 4 n00b is on a distinguished road
Send a message via MSN to n00b
this one works fine. i just don't know what the variable temp[80] is used for. in the text1.txt i've input only like 3 short lines or so, and if i change the array size to about 40 or so, it wont output a thing. at first i thought this was an array that accepts temp[n] characters from this text1 file, but it doesn't seem to be the case.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	char temp[80]; //what's this variable's function?
	fstream dat;
	dat.open("text1.txt",ios::in);
	if(!dat)
	{
		cout << "File does not exist!" << endl;
	}
	else
	{
		while(1)
		{
			dat.getline(temp,sizeof(temp),'\n');
			if(dat.eof()) break;
			cout << temp << endl;
		};
	};
	dat.close();
	return 1;
}
n00b is offline   Reply With Quote
Old Nov 3rd, 2006, 8:30 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
Extracts characters from the stream until either the delimiter delim (that would be your '\n') is found, the limit nCount–1 is reached (that would be your 'sizeof (temp)'), or end of file is reached. The characters are stored in the specified array (that would be 'temp') followed by a null terminator. If the delimiter is found, it is extracted but not stored.
:beard:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 3rd, 2006, 8:35 AM   #6
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>i just don't know what the variable temp[80] is used for.
Where did you steal the code from? But seriously, a better name would be "line" since it stores a line from the file.

>fstream dat;
>dat.open("text1.txt",ios::in);
It's easier to use the object that explicitly specifies the direction you want to go (ifstream or ofstream) so you don't have to pass the mode for a text file. You can also do all of this in the constructor:
ifstream dat ( "text1.txt" );
Mucho cleaner.

>cout << "File does not exist!" << endl;
Or you don't have permission to read it. A specific error is nice, but not when it might be completely wrong.

>while(1)
>{
> dat.getline(temp,sizeof(temp),'\n');
> if(dat.eof()) break;
> cout << temp << endl;
>};
Okay, getline can be used as a loop condition because it returns the stream state. So you don't need an infinite loop and you don't need to test dat.eof(). Also, the default delimiter is a newline, so unless you use something different, you can ignore the third argument. Finally, you should prefer using '\n' instead of endl. Most of the time the flush that endl performs is superfluous:
while ( dat.getline ( temp, sizeof temp ) )
  cout<< temp <<'\n';
>dat.close();
There's no need to close your stream if the object is about to go out of scope. The destructor takes care of cleaning up after itself, and that includes closing the file.

>return 1;
This means that the program failed. 0 means success. If you don't know what to return, don't return anything and 0 will be returned automagically.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is online now   Reply With Quote
Old Nov 3rd, 2006, 9:39 AM   #7
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 4 n00b is on a distinguished road
Send a message via MSN to n00b
so i was on the right track huh?
well, i got some slides from my university professor, there are some examples of code, but NO explanations whatsoever. so that's basically where i stole the code.
true, i like the ifstream better, saves a few letters

thanks for you help once again guys, i appreciate it.
n00b is offline   Reply With Quote
Old Nov 5th, 2006, 12:21 PM   #8
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 4 n00b is on a distinguished road
Send a message via MSN to n00b
i'm trying to reconstruct a program i wrote a few weeks ago using linked lists. but this time, i've tried to make it use files for destination and source of data that someone hipothetically might use for entering and tracking information about students.

i had lots of errors in the beginning, managed to overcome those problems i had. but it still won't work. i get a warning for a function call in line...
case 1: create_file(student); break;
saying that the local variable "student" is being used without having been initialized. besides that, when i run the program, and input all the data, it wont print a thing. where did i go wrong?


#include <iostream>
#include <fstream>
using namespace std;


void input(char *array)
{
        cin.getline(array, 150);
	if(cin.gcount()==1)
		cin.getline(array,150);
};


struct tstudent
{
	int number;
	char name[30];
	int degree;
};


void create_file(tstudent student)
{
        char FileName[20];
	cout << "Enter the name of your new file:" << endl;
	cin >> FileName;
	fstream dat ("data.txt",ios::out | ios::binary);
};


void input_data(tstudent student)
{
	fstream dat;
	char more='y';
	while((more=='y')||(more=='y'))
	{
		cout << "Student number: "; cin >> student.number;
		cout << "Student's name: "; input(student.name);
		cout << "College degree: "; cin >> student.degree;
		dat.write ((char *) &student,sizeof(student));
		cout <<"Wish to input more data (y/n) ? ";
		cin >> more;
	};
	dat.close();
};


void output_all_students(tstudent student)
{
	fstream dat ("data.txt",ios::in | ios::binary);
	while (1){
		dat.read ((char *) &student,sizeof(student));
		if (dat.eof()) break;
		cout << "Student number : " << student.number << endl;
		cout << "Student's name: " << student.name << endl;
		cout << "College degree: " << student.degree << endl;
	};
	dat.close(); 
};

void output_specific_student(tstudent student, int nr)
{
	int counter=0;
	dat.open("data.txt",ios::in | ios::binary);
	while (1){
		dat.read((char *) &student,sizeof(student));
		if (dat.eof()) break;
		if (student.number==nr){
			cout << "Student number: " << student.number << endl;
			cout << "Student's name: " << student.name << endl;
			cout << "College degree: " << student.degree << endl;
			counter++;
		}
	}
	if (counter==0)
		cout << "Student with number  " << number << " doesn't exist."<<endl;
	dat.close();
};


	
void main()
{
	int nr;
	tstudent student;
	int choice;
	do{
		cout << "1. Create file" << endl;
		cout << "2. Input data into file" << endl;
		cout << "3. Output all the data from the file" << endl;
		cout << "4. Output data for specific student" << endl;
		cout << "5. Exit the program" << endl;
		cin >> choice;
		
		switch(choice)
		{
		case 1:	create_file(student); break; 
		case 2:	input_data(student); break;
		case 3:	output_all_students(student); break;
		case 4:	cout << "Student's number:"; cin >> nr;
		            output_specific_student(student, nr); break;
		case 5: exit(0);
		};
	}while(choice!=5);
}

i haven't added those few lines to control if the input data matches the given variable, for eg only characters for names (char) and stuff so don't mind that stuff. i'm trying to set my thoughts on the big stuff right now

Last edited by n00b; Nov 5th, 2006 at 12:37 PM. Reason: spelling mistake :)
n00b is offline   Reply With Quote
Old Nov 5th, 2006, 12:46 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I'm just addressing one thing here, at least for this post.
Quote:
local variable "student" is being used without having been initialized
That seems eminently clear; don't let frustration buffalo you.
	tstudent student; Declaration, not set to anything (initialized).
	int choice;
	do{
		cout << "1. Create file" << endl;
		cout << "2. Input data into file" << endl;
		cout << "3. Output all the data from the file" << endl;
		cout << "4. Output data for specific student" << endl;
		cout << "5. Exit the program" << endl;
		cin >> choice;

No initialization anywhere here in the red, either....
		
		switch(choice)
		{
		case 1:	create_file(student); break; 
so....not initialized here either, unless you have 
a dead chicken and some expertise with voodoo.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 5th, 2006, 1:09 PM   #10
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>unless you have a dead chicken and some expertise with voodoo
Nope, but I could get it to work with a little foodoo. I try to avoid that stuff though, it's like selling your soul to the devil.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is online now   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB 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
Command Prompt SkyPioneer Coder's Corner Lounge 5 May 3rd, 2006 11:07 PM
How do Mingw .def files work thesis student C++ 0 Mar 10th, 2006 7:23 PM
Id3 tags of mp3 files, can't get it to work please help mleonid C++ 14 Aug 21st, 2005 10:22 PM
Using Fstream to read files Cipher C++ 7 Jun 15th, 2005 9:37 PM
shutil.copy corrupting files? TimL Python 0 Feb 20th, 2005 4:32 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:35 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC