| | |
fstream problem..
![]() |
•
•
Join Date: Sep 2007
Posts: 14
Reputation:
Solved Threads: 0
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.
Simple example..
C++ Syntax (Toggle Plain Text)
Open a files, while ch = getc(files) { ary[i] = ch << 2; display ary[i] }
You can do this quite elegantly with the STL... might be advanced, I dunno.
Edit: Note, this ignores whitespace.
Also change to
cpp Syntax (Toggle Plain Text)
#include <fstream> #include <vector> #include <string> #include <algorithm> bool char_sort_function( char a, char b ) { return a>b; } int main( int argc, char *argv[] ) { // File contains: This is a test! std::ifstream in( "test.txt", std::ios::binary ); if ( in ) { std::vector<char> vchar; // Read characters into vector vchar.insert( vchar.end(), std::istream_iterator<char>(in), std::istream_iterator<char>() ); // Sort them std::sort( vchar.begin(), vchar.end(), char_sort_function ); // Prints characters std::copy( vchar.begin(), vchar.end(), std::ostream_iterator<char>(std::cout, "\n") ); } return 0; }
Edit: Note, this ignores whitespace.
Also change
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <fstream.h> #include <string.h>
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string>
Last edited by twomers; Nov 23rd, 2008 at 10:03 am.
•
•
Join Date: Sep 2007
Posts: 14
Reputation:
Solved Threads: 0
]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: but the compiler says thar it cannot convert parameter 2 from 'class fstream' to 'char *'
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;
} Last edited by rumencho; Nov 23rd, 2008 at 10:22 am.
•
•
•
•
]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:but the compiler says thar it cannot convert parameter 2 from 'class fstream' to 'char *'#include <iostream.h> #include <fstream.h> int main() { fstream rufile ("c:\\ruru.txt",ios::in); char c; c= rufile.get (rufile,2); return 0; }
C++ Syntax (Toggle Plain Text)
if (rufile.get(c)) { // OK, you get this char } else { // end of file or i/o error }
![]() |
Similar Threads
- fstream Tutorial (C++)
- read to end of line problem (C)
- fstream.h + winsock2.h doesn't work? (C++)
- need help with programing with fstream to find area of triangle (C++)
- File processing problem (C++)
- Stack Queue Fstream (C++)
- Mode 13 graphics,Problem with io streams and new operator (C++)
- ** Need Help ** in a small C++ problem (C++)
Other Threads in the C++ Forum
- Previous Thread: help with double link list
- Next Thread: linked list pls pls help me
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets






