954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

fstream problem..

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;
}

rumencho
Newbie Poster
14 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Simple example..

Open a files,
while ch = getc(files)
{
  ary[i] = ch << 2;
  display ary[i]
}
cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 

You can do this quite elegantly with the STL... might be advanced, I dunno.

#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

#include <iostream.h>
#include <fstream.h>
#include <string.h>


to

#include <iostream>
#include <fstream>
#include <string>
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
 

Sort chars? What for?!..
Can you explain what do you want to do with a file contents?

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

]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 *'

rumencho
Newbie Poster
14 posts since Sep 2007
Reputation Points: 10
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: #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)!

if (rufile.get(c)) {
    // OK, you get this char
} else {
    // end of file or i/o error
}

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?

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You