Reading text files

This is a discussion on Reading text files within the C++ Programming forums, part of the General Programming Boards category; Well the ifstream constructor expects a string representing the name of the file to open. Passing it a char doesn't ...

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well the ifstream constructor expects a string representing the name of the file to open. Passing it a char doesn't work as you have seen. Pass it a char* or a string.c_str().

    Your function doesn't appear to be returning anything. Why have you declared it to return a double?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #17
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Someone just showed me arguments a few weeks ago, but below looks rights. But actually, no... that's not what you're attempting to do either -- I'll leave it to the experts.


    Code:
    #include "dll.h"
    #include <windows.h>   
    #include <fstream>
    
    
    export double func1(int argc,char *argv[])
    {
     for ( int i = 1 ; i < argc ; i++ ) {
    
           ifstream in (argv[i], ios::in | ios:: binary | ios::beg);
           ofstream out (argv[i], ios::out | ios:: binary | ios::app);
           char ch;
           while (in.get(ch)) {
           out << ch;
           }
           in.close();
           out.close();
    }
    }
    Last edited by Oldman47; 01-08-2007 at 03:04 PM.

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,326
    That code is for using command line arguments. You just have a function. We assume you want to pass the filenames to the function, is that correct? In that case, add one parameter for the input file name (a const char* would be appropriate) and another for the output filename (again, a const char*).

    Edit: I confused Oldman47 for the OP.

    To Xinco: A char holds a single character. You want to hold a string. You can use a const char* for that. You can also use the C++ string class, although there can be issues with using library classes across DLLs.
    Last edited by Daved; 01-08-2007 at 03:12 PM.

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Oldman47, your code is better, but the OP never said anything about command-line parameters and you're still not returning a double value. There's also no reason to be returning double. You could return void.

    Check out this tutorial: http://www.cprogramming.com/tutorial/lesson10.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #20
    Registered User
    Join Date
    Nov 2006
    Posts
    21
    Your function doesn't appear to be returning anything. Why have you declared it to return a double?
    Sorry, its for another part of the DLL, but im leaving that until i've got this bit sorted.
    Code:
    #include "dll.h"
    #include <windows.h>   
    #include <fstream>
    
    
    export double func1(int argc,char *argv[])
    {
     for ( int i = 1 ; i < argc ; i++ ) {
    
           ifstream in (argv[i], ios::in | ios:: binary | ios::beg);
           ofstream out (argv[i], ios::out | ios:: binary | ios::app);
           char ch;
           while (in.get(ch)) {
           out << ch;
           }
           in.close();
           out.close();
    }
    }
    That still returns the same error. Might the error lie in the compiler?(dev-c++)

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    7,326
    Oldman's code was wrong and not appropriate for your situation. Don't use it. The answer is much simpler.

    A char holds a single character. You want to hold a string. You can use a const char* for that. You can also use the C++ string class, although there can be issues with using library classes across DLLs. Make your two function parameters const char* instead of char.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 06:13 PM
  2. reading text files
    By stimpyzu in forum C++ Programming
    Replies: 11
    Last Post: 04-17-2004, 07:45 AM
  3. Reading spaces, carrage returns, eof from text files
    By thenrkst in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2003, 04:18 AM
  4. reading certain parts of text files
    By Captain Penguin in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 09:45 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21