Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » reading & writing a file
icon5.gif  reading & writing a file [message #32655] Tue, 31 May 2011 17:47 Go to next message
silverx is currently offline  silverx
Messages: 62
Registered: March 2011
Member
I am trying to read a file, once I get that done, I will want to be able to write a file out. Not only read/write a full line at one time, but will want to be able to read/write a binary file 1 byte at a time.

Are their any examples of reading a file, and writing a file?

It doesn't seem to find ifstream.

Any help with this, maybe other includes?

The errors are as follows:

C:\MyApps\readme\main.cpp(13) : error C2065: 'ifstream' : undeclared identifier
C:\MyApps\readme\main.cpp(13) : error C2065: 'file' : undeclared identifier
C:\MyApps\readme\main.cpp(14) : error C2448: 'Readini' : function-style initializer appears to be a function definition
C:\MyApps\readme\main.cpp(24) : error C2065: 'ifstream' : undeclared identifier
C:\MyApps\readme\main.cpp(24) : error C2146: syntax error : missing ';' before identifier 'inifile'
C:\MyApps\readme\main.cpp(24) : error C3861: 'inifile': identifier not found
C:\MyApps\readme\main.cpp(27) : error C2513: 'Upp::String' : no variable declared before '='
C:\MyApps\readme\main.cpp(27) : error C2065: 'inifile' : undeclared identifier
C:\MyApps\readme\main.cpp(28) : error C2065: 'inifile' : undeclared identifier
C:\MyApps\readme\main.cpp(28) : error C2228: left of '.eof' must have class/struct/union
type is ''unknown-type''
C:\MyApps\readme\main.cpp(27) : error C3861: 'Readini': identifier not found
C:\MyApps\readme\main.cpp(29) : error C2143: syntax error : missing ';' before '('
C:\MyApps\readme\main.cpp(29) : error C2059: syntax error : ')'
C:\MyApps\readme\main.cpp(32) : error C2065: 'inifile' : undeclared identifier
C:\MyApps\readme\main.cpp(32) : error C2228: left of '.close' must have class/struct/union
type is ''unknown-type''
readme: 1 file(s) built in (0:02.05), 2059 msecs / file, duration = 2106 msecs, parallelization 0%

There were errors. (0:02.79)


The code is as follows, right now just a blank layout, is all I have. The code I got from a C++ book so I just copied what they had for it:

#include "readme.h"


using namespace Upp;

#include <iostream>
#include <string>
#include <fstream>




String Readini(ifstream &file)
{
char buf[300]; // up to 300 bytes per line of input
file.getline(&(buf[0], 300);
return String(buf);
}

int DoLoadFunctions()
{
String file = "learn.ini";

ifstream inifile(file);
while (1)
{
String inline = Readini(inifile);
if (inifile.eof() == true)
break();

}
inifile.close();



PromptOK("This is a test");



return 0;

}




readme::readme()
{
// added to make window sizeable and zoomable
TopWindow::Sizeable(true);
TopWindow::Zoomable(true);

CtrlLayout(*this, "Read a File on Start up");
}

GUI_APP_MAIN
{

int lfreturn = DoLoadFunctions();

if (lfreturn == 0)
{
readme().Run();
}
else
{
// close it


}


}
Re: reading & writing a file [message #32657 is a reply to message #32655] Tue, 31 May 2011 19:38 Go to previous messageGo to next message
unknown user
Hello,

Please use [ CODE ] [ /CODE ] tags to mark code, so it get well formated.

Standard functions reside in std namespace so, you have to add
using namespace std;
after your includes
#include <iostream>
#include <string>
#include <fstream>
using namespace std;


In Readini you're missing an ')';
line:
file.getline(&(buf[0], 300);

You may write Readini like:
String Readini(ifstream &file)
{
	char buf[300]; // up to 300 bytes per line of input
	file.getline(buf, 300);
	return String(buf);
}


In DoLoadFunctions:
inline - is a C++ keyword, so you can't have a variable with that name;
break - it's a C++ keyword, not a function;
Correct DoLoadFunctions would be
int DoLoadFunctions()
{
	String file = "learn.ini";
	ifstream inifile(file);
	while (1)
	{
		String inl = Readini(inifile);
		if (inifile.eof() == true)
			break;
	}
	inifile.close();
	PromptOK("This is a test");
	return 0;
}


You may use U++ functions so you don't have to mix STL code with U++. If you want to read one byte at a time you may use use the following code:
FileIn fi;
fi.Open("learn.ini");
while (!fi.IsEof() && fi.IsOK())
	fi.Get(); // do something with it...


If you want to read an *.ini file you may use LoadIniFile (see help).

Andrei

[Updated on: Tue, 31 May 2011 19:40] by Moderator

Report message to a moderator

Re: reading & writing a file [message #32669 is a reply to message #32657] Wed, 01 June 2011 10:17 Go to previous messageGo to next message
silverx is currently offline  silverx
Messages: 62
Registered: March 2011
Member
I tried this and added a PromptOK(inl); so I can check what it is reading. Thanks.

When I run it nows gives me an box that has:

ERROR: Invalid UTF-8 sequence: `#

On the first line only, after that it just has a blank box instead of data to put out on other lines in the file.

The file is saved as unicode, as it will contain not only America characters, but will have Russian or Ukrainian characters and words in it as well. So it has to be saved in unicode.

Not sure if it is the PromptOK that has the issue or the read. If I don't put the PromptOK, I don't get any error, but I am not sure it is processing the file either.

David

Re: reading & writing a file [message #32672 is a reply to message #32669] Wed, 01 June 2011 11:37 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

silverx wrote on Wed, 01 June 2011 10:17

I tried this and added a PromptOK(inl); so I can check what it is reading. Thanks.

When I run it nows gives me an box that has:

ERROR: Invalid UTF-8 sequence: `#

Hi David,
The PromptOK() expects qtf formatted string as argument, please read the page on QTF. To pass a normal string to it, use PromptOK(DeQtf(inl)) to escape the characters that would be interpretted as qtf formating code or PromptOK("\1"+inl), to mark the entire string as non-qtf.

Honza
icon13.gif  Re: reading & writing a file [message #32676 is a reply to message #32672] Wed, 01 June 2011 13:49 Go to previous messageGo to next message
silverx is currently offline  silverx
Messages: 62
Registered: March 2011
Member
I tried that, and still got the same error. I tried that after trying that on another issue.

Any more suggestions?
Re: reading & writing a file [message #32677 is a reply to message #32676] Wed, 01 June 2011 14:03 Go to previous messageGo to next message
cbpporter is currently offline  cbpporter
Messages: 1406
Registered: September 2007
Ultimate Contributor
silverx wrote on Wed, 01 June 2011 14:49

I tried that, and still got the same error. I tried that after trying that on another issue.

Any more suggestions?

Yes. You may be trying to insert a invalid sequence into a Utf8 sequence, very common when trying o copy a 8bit encoding into Utf8 without converting it first.

Try dumping the binary values of inl somewhere and seeing if they are valid Utf. Easy way to see: if you have characters over 127, then the following character must follow special rules.
Re: reading & writing a file [message #32678 is a reply to message #32677] Wed, 01 June 2011 14:20 Go to previous messageGo to next message
silverx is currently offline  silverx
Messages: 62
Registered: March 2011
Member
It is a simple text file, created with notepad, and then saved as a unicode file, and the first character in the file is a #.
Re: reading & writing a file [message #32679 is a reply to message #32678] Wed, 01 June 2011 15:55 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3398
Registered: August 2008
Senior Veteran
Hello SilverX

I guess your file has a BOM, so you could try with LoadFileBOM() function.

You can be sure if your file begins with sequences like:

Encoding 	Representation (decimal) 
UTF-8 		239 187 191 
UTF-16 (BE) 	254 255 
UTF-16 (LE) 	255 254 
UTF-32 (BE) 	0 0 254 255 
UTF-32 (LE) 	255 254 0 0 
UTF-7 		43 47 118, and one of the following: [ 56 | 57 | 43 | 47 ] 
UTF-1 		247 100 76 
UTF-EBCDIC 	221 115 102 115 
SCSU 		14 254 255 
BOCU-1 		251 238 40 optionally followed by 255 
GB-18030 	132 49 149 51 



Best regards
Iñaki
icon7.gif  Re: reading & writing a file [message #32710 is a reply to message #32679] Thu, 02 June 2011 20:49 Go to previous messageGo to next message
silverx is currently offline  silverx
Messages: 62
Registered: March 2011
Member
Thank you for this information.

You are correct, as I found a hex viewer and did look at the hex of the file it was FF FE to start with.

And I look at the function you listed LoadFileBOM, and found it would be better for me to use the LoadFileBOMW instead.

The downside to this is it reads it in as a single WString or String, and not a line at a time.

The reason I decided on the LoadFileBOMW is I need to convert it back to a single line of input then take certain bytes out of it, and need to do a select of bytes based upon position in the line.



Re: reading & writing a file [message #32712 is a reply to message #32710] Thu, 02 June 2011 22:03 Go to previous message
koldo is currently offline  koldo
Messages: 3398
Registered: August 2008
Senior Veteran
Excellent!

Best regards
Iñaki
Previous Topic: GridCtrl out-of-bound crash with test case
Next Topic: using a file chooser (dir chooser??) to get a dir and not a file
Goto Forum:
  


Current Time: Fri Nov 01 00:48:25 CET 2024

Total time taken to generate the page: 0.04587 seconds