473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Input from file instead of cin

11
Hi coders, I need your help for a program that I'm doing!
I need to parse a text file, so I open it, read it and put the text in a buffer:
Expand|Select|Wrap|Line Numbers
  1.         int len;
  2.     char *buff;
  3.  
  4.     ifstream is;
  5.     is.open ("text.txt", ios::binary );
  6.  
  7.     is.seekg (0, ios::end);
  8.     len= is.tellg();
  9.     is.seekg (0, ios::beg);
  10.  
  11.     buffer = new char [len];
  12.  
  13.         is.read (buff,len);
Now I found this code:
Expand|Select|Wrap|Line Numbers
  1.     istreambuf_iterator<char> bit(cin), eit; // get stdin iterators
  2.     Parsing test(bit, eit);  // parse and load the text
  3.         printFile(&test);   // print the text structure
and here my questions:
1) istreambuf_iterator<char> bit(cin), eit; ------> "cin" is the "keyboard" input??
2) how can I modify this code to use the content of the file instead of the "keyboard" input?
Apr 29 '07 #1
9 2056
JosAH
11,448 Expert 8TB
Hi coders, I need your help for a program that I'm doing!
I need to parse a text file, so I open it, read it and put the text in a buffer:
Expand|Select|Wrap|Line Numbers
  1.         int len;
  2.     char *buff;
  3.  
  4.     ifstream is;
  5.     is.open ("text.txt", ios::binary );
  6.  
  7.     is.seekg (0, ios::end);
  8.     len= is.tellg();
  9.     is.seekg (0, ios::beg);
  10.  
  11.     buffer = new char [len];
  12.  
  13.         is.read (buff,len);
Now I found this code:
Expand|Select|Wrap|Line Numbers
  1.     istreambuf_iterator<char> bit(cin), eit; // get stdin iterators
  2.     Parsing test(bit, eit);  // parse and load the text
  3.         printFile(&test);   // print the text structure
and here my questions:
1) istreambuf_iterator<char> bit(cin), eit; ------> "cin" is the "keyboard" input??
2) how can I modify this code to use the content of the file instead of the "keyboard" input?
1) Yep, cin is an istream attached to the standard input (the keyboard usually).

2) replace 'cin' by your 'is'. 'is' is also a proper istream.

kind regards,

Jos
Apr 29 '07 #2
Arks
11
1) Yep, cin is an istream attached to the standard input (the keyboard usually).

2) replace 'cin' by your 'is'. 'is' is also a proper istream.

kind regards,

Jos
Thank you for the quick answer, as I thought I have to replace 'cin' with 'is'.
Unfortunatelly I think that the function that has to print the content of the file doesn't work and I have no idea how to fix it!
Apr 29 '07 #3
Arks
11
Is anyone able to tell me quite specifically what this code does??
Any help is appreciated!!

Expand|Select|Wrap|Line Numbers
  1. void printMimeStructure(MimeEntity* pMe, int tabcount = 0)
  2. {
  3.     Header& h = pMe->header();                   
  4.     for(int c = tabcount; c > 0; --c)            
  5.         cout << "    ";                            
  6.     cout << h.from() << endl;
  7.  
  8.     MimeEntityList& parts = pMe->body().parts(); 
  9.  
  10.     // cycle on sub entities list and print info of every item
  11.     MimeEntityList::iterator mbit = parts.begin(), meit = parts.end();
  12.     for(; mbit != meit; ++mbit)
  13.         printMimeStructure(*mbit, 1 + tabcount);
  14. }
I think that it parses a mail file and print the FROM, am I right??
Cause I'm trying to pass to this function a file, that I open in main(), using this:

Expand|Select|Wrap|Line Numbers
  1.         //my posted code
  2.         ifstream is;
  3.     is.open ("text.txt", ios::in );
  4.  
  5.     is.seekg (0, ios::end);
  6.     len= is.tellg();
  7.     is.seekg (0, ios::beg);
  8.  
  9.     buff= new char [len];
  10.  
  11.     is.read (buff,len);
  12.  
  13.     istreambuf_iterator<char> test1(is), test2;
  14.     int VarTest = imTestP | imTestE;
  15.     MimeEntity me(test1, test2, VarTest );
  16.         printMimeStructure(&me);
But it doesn't parse anything!
In fact instead of printing the From field it prints '/'.
Something wrong in my posted code??
Apr 29 '07 #4
Arks
11
No hints!?
Apr 29 '07 #5
Arks
11
I've done these passes, but the funny things is:
1) Start without Debugging;
2) VS2k5 opens the DOS window;
3) I can see only the flashing _ of DOS and nothing else;
4) after some seconds the system is slowing down and it freezes, have to restart!!

I think that the problem is here:
Expand|Select|Wrap|Line Numbers
  1.     MimeEntity me(bit,eit);                            // parse and load message
  2.         printMimeStructure(&me);                        // print msg structure
In fact if I comment out these lines, I can see the:
"Press any key to continue . . ."
nothing else but the program exits instead of doing an infinite loop!
May 1 '07 #6
JosAH
11,448 Expert 8TB
Is anyone able to tell me quite specifically what this code does??
Any help is appreciated!!
It is next to impossible to tell what an almost random piece of code is supposed
to do; especially if the presumed author of that code also doesn't know what it
does or is supposed to do. C++ doesn't help you one bit here because of all
that fancy operator overloading. Any guess is as good as another.

kind regards,

Jos
May 1 '07 #7
Arks
11
It is next to impossible to tell what an almost random piece of code is supposed
to do; especially if the presumed author of that code also doesn't know what it
does or is supposed to do. C++ doesn't help you one bit here because of all
that fancy operator overloading. Any guess is as good as another.

kind regards,

Jos
The funny thing is that the presumed author isn't me, but the author that releases the code that generates the library, which I'd like to use.
But I don't understand how this code doesn't work at all (the one that the author posted on his homepage).

Btw thank you for the answer! Bye
May 1 '07 #8
JosAH
11,448 Expert 8TB
The funny thing is that the presumed author isn't me, but the author that releases the code that generates the library, which I'd like to use.
But I don't understand how this code doesn't work at all (the one that the author posted on his homepage).

Btw thank you for the answer! Bye
You do know that the majority published on home pages is just trash don't you?
I'd say: contact the author about this piece of code. All the best.

kind regards,

Jos
May 1 '07 #9
Arks
11
You do know that the majority published on home pages is just trash don't you?
I'd say: contact the author about this piece of code. All the best.

kind regards,

Jos
No, if I publish something on my blog (C++ ideas or code) I release working code! :D

Thank you again!
May 1 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Oeln | last post by:
If I want to check for input of an integer I've got the following (I get the form input with $input = "$_POST"): if(!ereg("^+$",$_POST)) { echo "Input is incomplete or incorrect."; } If,...
0
by: Gregory Lielens | last post by:
Hello all, I am not sure this is the correct list to ask as my problem is really related to python low level implementation, but as I do not have found a python.dev list, here it is :-) We...
6
by: Nick R | last post by:
I need to know how to input bits from a *.wav file. I have tried this: #include <fstream> .... ifstream fin; fin.open("/root/foofile.wav", ios::in|ios::binary); and then inputting...
2
by: sam | last post by:
Hi, I've been buried in xsl and xslt articles for several days now, and am still unsure as to what I need to do... Basically, my vb.net app loads up an XML file from an external source...
5
by: google | last post by:
first, a little background... i have a C program which preprocesses some data, and then outputs the results into a text file. that text file, in turn, is used as input to a FORTRAN...
48
by: Michel Rouzic | last post by:
I know it must sound like a newbie question, but I never really had to bother with that before, and I didn't even find an answer in the c.l.c FAQ I'd like to know what's the really proper way...
3
by: dei3cmix | last post by:
Hey, I am having a problem with a program I am working on. Basically, the first part of the program gets input from a file using cin.getline. Then the second part, (still in the same main as the...
2
by: Ed Jay | last post by:
I'm dynamically creating several form input elements: mValue = integer constant; for(var j = 0; j < mValue; j++) { target = "imgCn"+ j; eName = "myFile"; eName = eName+jj;...
116
by: dmoran21 | last post by:
Hi All, I am working on a program to take input from a txt file, do some calculations, and then output the results to another txt file. The program that I've written compiles fine for me, however,...
26
by: Bert | last post by:
Hi, I'm unhappy: why doesn't this work? char enc; char temp; for(int i=0;i<10000;i++){ fscanf(in,"%s",&temp); if(temp=='#')break; else
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.