/*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...*/

#include <fstream>  

using namespace std;

int main([b]int argc, char **argv[/b])
{
        ifstream file;

        if (argc == 2) {
                file.open([b]argv[1][/b]);
                .....
        }

        .....

        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

[[b][u]001[/u][/b]]     #include <fstream>
[[b][u]002[/u][/b]]
[[b][u]003[/u][/b]]     using namespace std;
[[b][u]004[/u][/b]]
[[b][u]005[/u][/b]]     int main(int argc, char **argv)
[[b][u]006[/u][/b]]     {
[[b][u]007[/u][/b]]             ifstream file;
[[b][u]008[/u][/b]]
[[b][u]009[/u][/b]]             if (argc == 2) {
[[b][u]010[/u][/b]]                     file.open(argv[1]);
[[b][u]011[/u][/b]]                     /* ..... */
[[b][u]012[/u][/b]]             }
[[b][u]013[/u][/b]]
[[b][u]014[/u][/b]]             /* ..... */
[[b][u]015[/u][/b]]
[[b][u]016[/u][/b]]             return 0;
[[b][u]017[/u][/b]]     }

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?

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
*/

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.

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;

How you do this will vary depending on your whitespace needs, but the above should suffice. Concerning integers, can you be more specific about what you want to do? What does the file look like, how do you want to process it, and what should the output be?

#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.

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.

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
*/
This article has been dead for over six months. Start a new discussion instead.