|
|
Home » Community » Newbie corner » reading & writing a file
reading & writing a file [message #32655] |
Tue, 31 May 2011 17:47 |
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 |
|
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 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 #32672 is a reply to message #32669] |
Wed, 01 June 2011 11:37 |
|
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
|
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Fri Nov 01 00:48:25 CET 2024
Total time taken to generate the page: 0.04587 seconds
|
|
|