943,616 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2727
  • C++ RSS
May 12th, 2005
0

Using data i read from *.* files

Expand Post »
/*I used and modified a piece of code i found in here and my main(){} looks like this/

C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. HANDLE hConsole;
  4. char welcome_l1[100];
  5. char welcome_l2[100];
  6. char welcome_l3[100];
  7. char welcome_l4[100];
  8. unsigned short k=120;
  9. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  10. ifstream file("ver");
  11. file.getline(welcome_l1, 100);
  12. file.getline(welcome_l2, 100);
  13. file.getline(welcome_l3, 100);
  14. file.getline(welcome_l4, 100);
  15. SetConsoleTextAttribute(hConsole, k);
  16. cout<<welcome_l1 <<"\n";
  17. cout<<welcome_l2 <<"\n";
  18. cout<<welcome_l3 <<"\n";
  19. cout<<welcome_l4 <<"\n";
  20. SetConsoleTextAttribute(hConsole, 8);
  21. return 0;
  22. }
<< 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...*/
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Prahaai is offline Offline
59 posts
since May 2005
May 12th, 2005
0

Re: Using data i read from *.* files

#include <fstream>  

using namespace std;

int main(int argc, char **argv)
{
        ifstream file;

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

        .....

        return 0;
}

Or something
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
May 12th, 2005
0

Re: Using data i read from *.* files

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

[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]     }
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
May 17th, 2005
0

Re: Using data i read from *.* files

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Prahaai is offline Offline
59 posts
since May 2005
May 17th, 2005
0

Re: Using data i read from *.* files

Quote 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:
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"
*/

Quote 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?
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
*/
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 20th, 2005
0

Re: Using data i read from *.* files

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
C++ Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Prahaai is offline Offline
59 posts
since May 2005
May 20th, 2005
0

Re: Using data i read from *.* files

C++ Syntax (Toggle Plain Text)
  1. int NoLinesIgnored, NoCharsIgnored, NoCharsWanted;
  2. std::string s;
  3.  
  4. // ...
  5.  
  6. while (--NoLinesIgnored >= 0) {
  7. // Read and discard a line
  8. file.ignore(std::numeric_limits<streamsize>::max(), '\n');
  9. }
  10.  
  11. // Read and discard N characters
  12. file.ignore(NoCharsIgnored);
  13.  
  14. // Read and save N characters
  15. 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?
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
May 20th, 2005
0

Re: Using data i read from *.* files

#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
*/
Quote ...
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.
Last edited by Dave Sinkula; May 20th, 2005 at 3:49 pm. Reason: D'oh! Pokey and of lesser quality.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 21st, 2005
0

Re: Using data i read from *.* files

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Prahaai is offline Offline
59 posts
since May 2005
May 21st, 2005
0

Re: Using data i read from *.* files

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)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. using namespace std; // for homework and toy programs
  6.  
  7. int main()
  8. {
  9. ifstream file("data.dat");
  10. string info;
  11. for ( ;; )
  12. {
  13. file.ignore(3);
  14. if ( file >> info )
  15. {
  16. istringstream iss(info);
  17. int number;
  18. if ( iss >> number )
  19. {
  20. cout << "number = " << number << endl;
  21. }
  22. else
  23. {
  24. cout << "info = " << info << endl;
  25. }
  26. }
  27. else
  28. {
  29. break;
  30. }
  31. }
  32. return 0;
  33. }
  34.  
  35. /* my output
  36. number = 1
  37. number = 100
  38. number = 79
  39. info = Vasilica
  40. number = 1000
  41. number = 1000
  42. number = 2
  43. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: fstream isnt getting the data right
Next Thread in C++ Forum Timeline: Need help with my console database app, program reading or writing incorrectly





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC