RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 2255 | Replies: 9 | Thread Tools  Display Modes
Reply
Join Date: May 2005
Location: Romania.
Posts: 51
Reputation: Prahaai is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Prahaai's Avatar
Prahaai Prahaai is offline Offline
Junior Poster in Training

Help Using data i read from *.* files

  #1  
May 12th, 2005
/*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...*/
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2003
Location: Austin, TX
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 0
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: Using data i read from *.* files

  #2  
May 12th, 2005
#include <fstream>  

using namespace std;

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

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

        .....

        return 0;
}

Or something
Reply With Quote  
Join Date: Aug 2003
Location: Austin, TX
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 0
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: Using data i read from *.* files

  #3  
May 12th, 2005
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]     }
Reply With Quote  
Join Date: May 2005
Location: Romania.
Posts: 51
Reputation: Prahaai is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Prahaai's Avatar
Prahaai Prahaai is offline Offline
Junior Poster in Training

Re: Using data i read from *.* files

  #4  
May 17th, 2005
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?
Reply With Quote  
Join Date: Apr 2004
Posts: 3,808
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Using data i read from *.* files

  #5  
May 17th, 2005
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"
*/

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
*/
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
Reply With Quote  
Join Date: May 2005
Location: Romania.
Posts: 51
Reputation: Prahaai is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Prahaai's Avatar
Prahaai Prahaai is offline Offline
Junior Poster in Training

Re: Using data i read from *.* files

  #6  
May 20th, 2005
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.
Reply With Quote  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: Using data i read from *.* files

  #7  
May 20th, 2005
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?
Reply With Quote  
Join Date: Apr 2004
Posts: 3,808
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Using data i read from *.* files

  #8  
May 20th, 2005
#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.
Last edited by Dave Sinkula : May 20th, 2005 at 3:49 pm. Reason: D'oh! Pokey and of lesser quality.
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
Reply With Quote  
Join Date: May 2005
Location: Romania.
Posts: 51
Reputation: Prahaai is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Prahaai's Avatar
Prahaai Prahaai is offline Offline
Junior Poster in Training

Re: Using data i read from *.* files

  #9  
May 21st, 2005
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.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,808
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Using data i read from *.* files

  #10  
May 21st, 2005
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
*/
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 10:20 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC