Thread: Write the .txt "filename" into a file.

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    Write the .txt "filename" into a file.

    I am trying to put the filename that I am reading from to the file using this code:
    The file is named ABC and this is what I want to write to the file: "file2".

    I am trying to type in << myfile << but nothing is written to the file.
    I want it to recognice what is written in the paratheses after the declaration of myfile. Is this possible in any way ?

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    
    using namespace std;
    
    int main () 
    
    { 
    	std::string Date;
    	int Time = 0;
    	char Comma;
    	
    	ofstream Test;
    	Test.open ("file2.txt");
    	ifstream myfile ("ABC.txt");
    
    	getline(myfile, Date, ','); 
    	myfile >> Time;		        // 2111
    	myfile >> Comma;
    
    	if (Time == 2100)            
    	{
    	Test << myfile << Time <<"\n";    
    	}
    
            return 0;
    }
    Last edited by Coding; 01-06-2008 at 01:23 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Coding View Post
    I am trying to type in << myfile << but nothing is written to the file.
    Well, you are for one thing trying to write an ifstream to the ostream, which I don't think is acceptable.

    I want it to recognice what is written in the paratheses after the declaration of myfile. Is this possible in any way ?
    Huh?

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    
    using namespace std;
    
    int main () 
    { 
    	std::string Date;
    	int Time = 0;
    	char Comma;
    	
    	ofstream Test;
    	Test.open ("file2.txt");
    	ifstream myfile ("ABC.txt");
    
    	getline(myfile, Date, ','); 
    	myfile >> Time;		        // 2111
    	myfile >> Comma;
    
    	if (Time == 2100)            
    	{
    		Test << myfile << Time <<"\n";    
    	}
    
    	return 0;
    }
    And as for fixing the indentation a little...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I see... Thanks for the corrections.
    Could it be possible while reading the Date and Time from the file to also identify the filename from where it is red.
    Could it be any approach to do it this way. I dont know if this makes any different ?
    Last edited by Coding; 01-06-2008 at 01:43 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you mean by "identify the filename"? It's "ABC.txt" -- you have to know it beforehand in order to open your ifstream.

    Edit: and I suppose if I take account the title of the thread, you can then do Test << "ABC.txt". If you want it to be more general, you can use a char[] as the name of the file to open, something like
    Code:
    char filename[] = "ABC.txt";
    and use the variable name in both places.
    Last edited by tabstop; 01-06-2008 at 01:49 PM.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes you are right.. I think I am mixing this up with another soulution for something else I want to do.. I do have the filename already in this case, thats correct...
    Thanks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM