There's no error. The file is (apparently) being created, but I cannot locate the file either in the "BIN" folder, or the directory that it is supposed to be in.

Have you tried searching for the file elsewhere on the drive, and did it show up anywhere?

Yes, I searched, but couldn't find it.

The file is not being created at all. :(

OK, when you open the file, you should check that the file is actually opened. The specific error code may give you some insight as to why it is failing.

report.open(fname, ios::out);
    if (!report.is_open())
    {
        cerr << "File " << fname << " could not be opened.";
    }

Similarly, test after read write out to the file, to see if the write was successful.

report << s.admno;
    if (report.fail())
    {
        cerr << "Unable to write the admission number to file " << fname;
        report.clear();
    }

I realize that this is tedious, but it is the best way to ensure that the file is written correctly.

On a possibly related note, I see that you are trying to write out the variable s directly to the file. You cannot effectively do this with a structure, however; all you'll get it the pointer to the structure, not the values inside of it. Even if it were to write the structure, the data would be useless, as any pointers or offsets would be invalid when you try to read it back. You need to write out the individual elements of the structure (a process called serialization) in order to get a useful value written to the file.

C and C++ do not provide direct support for serialization. It is however possible to write your own serialization functions, since both languages support writing binary data. Besides, compiler-based solutions, such as the ODB ORM system for C++, are capable of automatically producing serialization code with few or no modifications to class declarations. Another popular serialization framework is Boost.Serialization [5] from the Boost Framework.

How to do it?

In this case, it is simply a matter of writing out the individual elements of the structure in some appropriate order. It doesn't have to be anything fancy; a comma-separated values (CSV) file will do the trick, I would expect. Can you post the structure in question?

Here we go:

class report
{
public:
	void mainmenu();
	void generate();
	void del();
	void view();
	void readme();
	void close();
}r;

class student
{
public:
	char name[20];
	int admno, marks[5];
	float perc;
	void disp();
}s;

And I'll ask my teacher if she can have TC 4.5 installed in the school.

OK, then. For the student structure, add this to the student structure:

void write(std::ostream& outfile);

Then the implementation would go something like:

void student::write(std::ostream& outfile)
{
    outfile << admno << ',';
    outfile << name  << ','; 

    for (int i = 0; i < 5; i++)
    {
        outfile << marks[i] << ',';
    }

    outfile << perc << std::endl;
}

This is just an example of one way to do it, but it is probably the most straightforward, and has the advantage that the resulting file can be read in a standard text editor.

Hi

I know the problem now.

In TC3, the getcwd() shows the "current directory" that is set in TC (usually C:\TC\BIN ). Same is for getcurdir() .

That means that these two methods cannot work beyond the DOS environment in TC 3.

And that means my project is screwed. :(

Not necessarily, I hope. If you look at the Google book I'd linked earlier, you'll see that there was also a chdir() (change directory) function, which should let you use directories other than the one you started in. You'd still be limited to the DOS environment, but you could at least move around among the directories. Keep in mind also that directories with long names can still be referenced by shorter, mangled names, for example, "C:\Documents and Settings\" becomes "C:\DOCUME~1\" for DOS programs. If all else fails, you can put the Reports directory in the root directory, as "C:\Reports". At least I am hoping that should work.

Don't forget to check if the directory exists (using searchpath() , I think); if it doesn't, use mkdir() to create it before you use it.

Okay I'm back now.

My project has got an extension now. Need to submit in 2-3 days!

A few things: Custom location is not working in TC 3.0. getcwd() doesn't work as required.

Tried

sprintf(fname, "%s\\Reports\\%d.txt", directory, s.admno);

but doesn't work in TC 3.0.

Also, how does this snippet work?

if(remove(fname)!= 0)
	cout<<"Error deleting file.."<<endl;
else
	cout<<"File successfully deleted.."<<endl;

(fname is the name of the file)

I'm a little confused in this. If remove() is failed, then it should return 0. But seems like here it's the opposite.

Help would be really appreciated.

Could you please be more specific about how getcwd() and sprintf() are failing?

Sure.

Let's say my program is saved in path:

C:\Program Files\Reports\

In Tubro C++ 3,

getcwd(directory, sizeof(directory));

command would not add the above code, but rather, the directory path added in TC setting. By default and in most cases:

C:\TC\BIN

In TC3, the getcwd() shows the "current directory" that is set in TC (usually C:\TC\BIN ). Same is for getcurdir() .

That means that these two methods cannot work beyond the DOS environment in TC 3.

For what it's worth, most commercial Windows software puts "hidden" data in %APPDATA%/<ProgramName>/whateverYouWant. User files, by default, are placed somewhere under "My Documents" (or "Documents and Settings/<user>" in XP). Search for SHGetSpecialFolderPath function on msdn.microsoft.com for more info on how to get the actual directory for constant-named conceptual folders.

I'm not a TurboC user, so I can't help with compiler-specific limitations.

Thanks for the reply, but again, TC doesn't support SHGetSpecialFolderPath. :(

As for how to obtain the text of the window to stay where you want to keep it, and scroll to where you want, you may have to code yourself. I am not a user of TC to all these functions are not part of standard C + +.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.