Using data i read from *.* files
/*I used and modified a piece of code i found in here and my main(){} looks like this:*/
int main()
{
HANDLE hConsole;
char welcome_l1[100];
char welcome_l2[100];
char welcome_l3[100];
char welcome_l4[100];
unsigned short k=120;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
ifstream file("ver");
file.getline(welcome_l1, 100);
file.getline(welcome_l2, 100);
file.getline(welcome_l3, 100);
file.getline(welcome_l4, 100);
SetConsoleTextAttribute(hConsole, k);
cout<<welcome_l1 <<"\n";
cout<<welcome_l2 <<"\n";
cout<<welcome_l3 <<"\n";
cout<<welcome_l4 <<"\n";
SetConsoleTextAttribute(hConsole, 8);
return 0;
}
<< moderator edit: added [code][/code] tags and indented code >>
/*My -ver- file prints out the version of the program and the author. I have a big problem though if i want to use variables stored in files with differend extensions, like "*.rc" for example. I want my program to be extremely flexible, i dont want to recompile it 1000 times. So is anyone here able to suggest how to store && use (and why not modify) data from external files?
I want to mention that my program is and will not be commercial...*/
Prahaai
Junior Poster in Training
59 posts since May 2005
Reputation Points: 10
Solved Threads: 0
Thank you very much subtronic. Thank you for the reply, but, well... i cant really understand how i use it... i am quite confused, sorry, i am not that good to understand where exactly i put the name of the file i have to read?
And i dont understand why you put that (argc == 2)... :cry:
And one more thing; it must be quite simple: how can i read "10" characters, after "5" characters from the beggining of the line "3" of a text file?
Prahaai
Junior Poster in Training
59 posts since May 2005
Reputation Points: 10
Solved Threads: 0
i cant really understand how i use it... i am quite confused, sorry, i am not that good to understand where exactly i put the name of the file i have to read?
And i dont understand why you put that (argc == 2)... :cry:
From the command line.
#include <iostream>
int main(int argc, char *argv[])
{
int i;
for (i = 1; i < argc; ++i)
{
std::cout << "argv[" << i << "] = \"" << argv[i] << "\"\n";
}
return 0;
}
/* my output
C:\Test>testpp filename
argv[1] = "filename"
*/
And one more thing; it must be quite simple: how can i read "10" characters, after "5" characters from the beggining of the line "3" of a text file?
Maybe like this.
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char *argv[])
{
if ( argc > 1 )
{
std::ifstream file(argv[1]);
std::string line;
if ( getline(file, line) && /* read and discard line 1 */
getline(file, line) && /* read and discard line 2 */
getline(file, line) ) /* read line 3 */
{
std::cout << line.substr(5, 10) << std::endl;
}
}
return 0;
}
/* my output
C:\Test>testpp testpp.cpp
ude <strin
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Dear "Dave Sinkula", i cant stop wondering how did that code magically compile... I dont understand anything of it... i am probably much more a noob than i thought.
I wanted to use your function as an external, NON-main function, so that i will be able to call from any other function. Of course, i failed...
Anyway, the function you gave me has constant values in
std::cout << line.substr(5, 10) << std::endl;
so it is too inflexible.
Can you, or anyone else help me read "NoCharsWanted" characters after "NoCharsIgnored" ignored characters, after "NoLinesIgnored" ignored lines (assuming that the characters i need are on the same line, i mean, i only want to read from one line), from MyFile.txt file?
And there is another problem i have: how can i do the exact same thing, but with INTEGERS? I want to read a few integers from a file...
Thank you very much.
Prahaai
Junior Poster in Training
59 posts since May 2005
Reputation Points: 10
Solved Threads: 0
#include <iostream>
#include <fstream>
#include <string>
std::string foo(const char *filename,
int NoCharsWanted, int NoCharsIgnored, int NoLinesIgnored)
{
std::ifstream file(filename);
std::string line;
for ( int i = 0; i < NoLinesIgnored; ++i )
{
if ( !getline(file, line) )
{
return "";
}
}
return line.substr(NoCharsIgnored, NoCharsWanted);
}
int main()
{
std::string text = foo(__FILE__, 10, 5, 3);
std::cout << text << std::endl;
return 0;
}
/* my output
ude <strin
*/
And there is another problem i have: how can i do the exact same thing, but with INTEGERS?
[Of course. It always seems that finding the question is more difficult than finding the answer when you're starting out.]
How about posting a small sample of a file that you are trying to parse. Then describe the input and output you would like.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
My externam file: "data.dat":
1..1
2..100
3..79
4..Vasilica
//... here are more lines, but the idea is that i always have to jump over 3
//characters to get the data i need, but for the sake of flexibility, the variable
//NoCharactersIgnored is very useful...
11.1000
12.1000
13.2
//and so on. I sometimes need to load the data as Int and other times, load
//the data as Char... i have already seen how to load it as char, but what about //int?
//Thank you very very very much for the help.
Prahaai
Junior Poster in Training
59 posts since May 2005
Reputation Points: 10
Solved Threads: 0
Read the info as a string. If you can convert it to an int, it's probably an int; if you can't it probably isn't.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std; // for homework and toy programs
int main()
{
ifstream file("data.dat");
string info;
for ( ;; )
{
file.ignore(3);
if ( file >> info )
{
istringstream iss(info);
int number;
if ( iss >> number )
{
cout << "number = " << number << endl;
}
else
{
cout << "info = " << info << endl;
}
}
else
{
break;
}
}
return 0;
}
/* my output
number = 1
number = 100
number = 79
info = Vasilica
number = 1000
number = 1000
number = 2
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314