| | |
Using data i read from *.* files
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
/*I used and modified a piece of code i found in here and my main(){} looks like this
/
<< 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...*/
/ C++ Syntax (Toggle Plain Text)
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; }
/*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...*/
#include <fstream> using namespace std; int main(int argc, char **argv) { ifstream file; if (argc == 2) { file.open(argv[1]); ..... } ..... return 0; }
Or something
Neat I wrote a little C program to format my DaniWeb example code, such that it is formatted in the Kerningham and Ritchie C-style tradition with 8-space tabs and 80 character columns. And to make it harder to just cut-and-paste code and to also add to the readability, line numbering with some sexy bbCode... and a useless timestamp for good measure
Generated: Thu May 12 14:35:27 2005
Generated: Thu May 12 14:35:27 2005
[001] #include <fstream> [002] [003] using namespace std; [004] [005] int main(int argc, char **argv) [006] { [007] ifstream file; [008] [009] if (argc == 2) { [010] file.open(argv[1]); [011] /* ..... */ [012] } [013] [014] /* ..... */ [015] [016] return 0; [017] }
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?
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?
•
•
•
•
Originally Posted by Prahaai
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:
#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"
*/•
•
•
•
Originally Posted by Prahaai
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?
#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
*/ "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
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
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.
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
C++ Syntax (Toggle Plain Text)
std::cout << line.substr(5, 10) << std::endl;
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.
C++ Syntax (Toggle Plain Text)
int NoLinesIgnored, NoCharsIgnored, NoCharsWanted; std::string s; // ... while (--NoLinesIgnored >= 0) { // Read and discard a line file.ignore(std::numeric_limits<streamsize>::max(), '\n'); } // Read and discard N characters file.ignore(NoCharsIgnored); // Read and save N characters file >> std::setw(NoCharsWanted) >> s;
#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?
How about posting a small sample of a file that you are trying to parse. Then describe the input and output you would like.
Last edited by Dave Sinkula; May 20th, 2005 at 3:49 pm. Reason: D'oh! Pokey and of lesser quality.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
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.
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.
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.
C++ Syntax (Toggle Plain Text)
#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 */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Data Read Error at startup after Power Failure (Windows NT / 2000 / XP)
- Write and Read text files (Java)
- Hi, How to access and read .csv files in C++ (C++)
- Can't read my .doc files in new comp. w/XP (Windows NT / 2000 / XP)
- How do i flush a variable (C)
Other Threads in the C++ Forum
- Previous Thread: fstream isnt getting the data right
- Next Thread: Need help with my console database app, program reading or writing incorrectly
Views: 2545 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






