fstream problem..

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 14
Reputation: rumencho is an unknown quantity at this point 
Solved Threads: 0
rumencho rumencho is offline Offline
Newbie Poster

fstream problem..

 
0
  #1
Nov 23rd, 2008
Hi all. I want to learn how to manipulate .txt files through C++ program. I want to be able to search trough chars in .txt file,sort them etc. I think the best way for this,should be,loading .txt file in char array and then manipulating the elements. but I don't know how..




#include <iostream.h>
#include <fstream.h>
#include <string.h>
int main()

{

               char array[100000];
	
	fstream ru ("c:\\ruru.txt",ios::out); 

	for (int i=0;i<10000;i++)
	
	{  ru << "rordo"; }  // write to file

                 array[ ] =ru  //obviously this doesn't work


return 0;
}
Last edited by rumencho; Nov 23rd, 2008 at 6:12 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: fstream problem..

 
0
  #2
Nov 23rd, 2008
Simple example..
  1. Open a files,
  2. while ch = getc(files)
  3. {
  4. ary[i] = ch << 2;
  5. display ary[i]
  6. }
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,871
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: fstream problem..

 
0
  #3
Nov 23rd, 2008
You can do this quite elegantly with the STL... might be advanced, I dunno.
  1. #include <fstream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. bool char_sort_function( char a, char b ) {
  7. return a>b;
  8. }
  9.  
  10. int main( int argc, char *argv[] ) {
  11.  
  12. // File contains: This is a test!
  13. std::ifstream in( "test.txt", std::ios::binary );
  14.  
  15. if ( in ) {
  16. std::vector<char> vchar;
  17.  
  18. // Read characters into vector
  19. vchar.insert( vchar.end(), std::istream_iterator<char>(in), std::istream_iterator<char>() );
  20.  
  21. // Sort them
  22. std::sort( vchar.begin(), vchar.end(), char_sort_function );
  23.  
  24. // Prints characters
  25. std::copy( vchar.begin(), vchar.end(), std::ostream_iterator<char>(std::cout, "\n") );
  26. }
  27.  
  28. return 0;
  29. }

Edit: Note, this ignores whitespace.
Also change
  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <string.h>
to
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
Last edited by twomers; Nov 23rd, 2008 at 10:03 am.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: fstream problem..

 
0
  #4
Nov 23rd, 2008
Sort chars? What for?!..
Can you explain what do you want to do with a file contents?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 14
Reputation: rumencho is an unknown quantity at this point 
Solved Threads: 0
rumencho rumencho is offline Offline
Newbie Poster

Re: fstream problem..

 
0
  #5
Nov 23rd, 2008
]it doesn't work with getc, it says "getc- undeclared identifier" btw I'm using visual c++ compiler

I just want to take char from .txt file,and save it in char variable..or in array


I have also tried this way:
#include <iostream.h>
#include <fstream.h>

int main()
{ 
	fstream rufile ("c:\\ruru.txt",ios::in);
		char c;
	c= rufile.get (rufile,2);
	return 0;
}
but the compiler says thar it cannot convert parameter 2 from 'class fstream' to 'char *'
Last edited by rumencho; Nov 23rd, 2008 at 10:22 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: fstream problem..

 
0
  #6
Nov 23rd, 2008
Originally Posted by rumencho View Post
]it doesn't work with getc, it says "getc- undeclared identifier" btw I'm using visual c++ compiler

I just want to take char from .txt file,and save it in char variable..or in array

I have also tried this way:
#include <iostream.h>
#include <fstream.h>

int main()
{ 
	fstream rufile ("c:\\ruru.txt",ios::in);
		char c;
	c= rufile.get (rufile,2);
	return 0;
}
but the compiler says thar it cannot convert parameter 2 from 'class fstream' to 'char *'
The compiler said that it can't convert parameter 1 (first, not second)!
  1. if (rufile.get(c)) {
  2. // OK, you get this char
  3. } else {
  4. // end of file or i/o error
  5. }
Set cursor on a function name and press F1. Look at the function prototype. Do you want ask daniweb for every new C++ function parameters?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC