943,929 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 576
  • C++ RSS
Nov 23rd, 2008
0

fstream problem..

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rumencho is offline Offline
14 posts
since Sep 2007
Nov 23rd, 2008
0

Re: fstream problem..

Simple example..
C++ Syntax (Toggle Plain Text)
  1. Open a files,
  2. while ch = getc(files)
  3. {
  4. ary[i] = ch << 2;
  5. display ary[i]
  6. }
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Nov 23rd, 2008
0

Re: fstream problem..

You can do this quite elegantly with the STL... might be advanced, I dunno.
cpp Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <string.h>
to
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
Last edited by twomers; Nov 23rd, 2008 at 10:03 am.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Nov 23rd, 2008
0

Re: fstream problem..

Sort chars? What for?!..
Can you explain what do you want to do with a file contents?
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 23rd, 2008
0

Re: fstream problem..

]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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rumencho is offline Offline
14 posts
since Sep 2007
Nov 23rd, 2008
0

Re: fstream problem..

Click to Expand / Collapse  Quote originally posted by rumencho ...
]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)!
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

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: help with double link list
Next Thread in C++ Forum Timeline: linked list pls pls help me





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


Follow us on Twitter


© 2011 DaniWeb® LLC