Results 1 to 8 of 8

Thread: [Resolved] Can someone help me with this line of code?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    [Resolved] Can someone help me with this line of code?

    Code:
    TCHAR filebuf[256];
    
    _tprintf(_T("Directory = {%s}\n"), filebuf);

    I wish to convert this code into a char type or a char(That is not printed onto the screen).

    So basically:

    Somechar=filebuf;

    I've tried every way my brain can think of. I either get compiler errors, or i get weird HEX numbers on the screen (I'd say they are the memory addresses of the filebuf pointer). However, I can't get it working .

    I'm trying to find out the directory that the program is in when it's running (App.Path in VB terms), then writing it into a file. This code is the only thing stopping me. The path comes up on the screen. But I can't convert into another data type from here.
    Last edited by Slyke; Aug 25th, 2008 at 10:30 AM.

  2. #2
    Member
    Join Date
    Apr 2008
    Posts
    42

    Re: Can someone help me with this line of code?

    It already is in char type.
    What are you using to write to file? the fstream header? Or fopen//fwrite//fclose ?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Can someone help me with this line of code?

    I believe it's ofstream.

    Here's basically my code:

    Code:
    ofstream myfile (destFile);
    int lineCount;
    myfile << "";
    lineCount=0;
    string newstr;
    
    if (myfile.is_open())
    	{
    		lineCount++;
    		myfile << newstr << endl;
    		cout << "Line " << setw(4) << lineCount << ": " << newstr << endl;
    	}
    	else cout << "Unable to open file";
    	}
    		if (myfile.is_open())
    	{
    		myfile.close();
    	}
    }
    Basically, I need the exe path to go into a variable, into a parameter though a function, then into into newstr. The parameter requires it to be of type char *, but I need it in a char * variable first.
    Or in code terms:

    Code:
    char *file_path;
    *file_path = (_T("Directory = {%s}\n"), filebuf);
    So that it puts the path into the string instead of printing to the screen.

    I get the following error when trying to do that code:
    PHP Code:
    error C2440'=' cannot convert from 'TCHAR *' to 'char' 


    If you require the full code I will post it up, but I think I've answered your questions .
    Last edited by Slyke; Aug 25th, 2008 at 03:46 AM.

  4. #4
    Member
    Join Date
    Apr 2008
    Posts
    42

    Re: Can someone help me with this line of code?

    When I tried to compile some code with the line
    Code:
    *file_path = (_T("Directory = {%s}\n"), filebuf);
    it said _T() wasn't recognized. What headers are you including? It would just be better to post your entire code.

    But here's how I'd do it.

    Code:
    char cFile[512];
    DWORD dwFile;
    GetModuleFileName(NULL,cFile,dwFile);
    printf("Path: %s\n",cFile);
    That will retrieve the full path of the current exe, including the filename.
    E.G. D:\apps\file.exe

    It'd be easy to write a function to retrieve just the directory from a full file path.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Can someone help me with this line of code?

    Okay, here it is!

    Yep, I'm willing to try any way to get the path name into type char*. It needs to be passed into the doFiles function (Single \ stop it from working, so all directories need to have \\ separating the folders).


    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    #include <stdio.h>  
    #include <stdlib.h>  
    #include <tchar.h>  
    #include <windows.h>
    
    using namespace std;
    
    #define COUNTOF(x)  ( sizeof(x) / sizeof((x)[0]) )
    
    void doFiles(char sourceFile[], char destFile[], char findWhat[], char replaceWith[]);
    string replaceString(string &stringToSearch, const string findWhat, string replaceWith);
    string StringToLower(string strToConvert);
    BOOL get_module_directory(TCHAR *obuf, size_t osize);
    char WcharToString(TCHAR* pTchar);
    char *TCHARTochar(TCHAR *theTCHAR);
    
    
    int main()
    {
    	TCHAR filebuf[256];
    	char *file_path;
        
    	if ( get_module_directory(filebuf, COUNTOF(filebuf)) )
    	{
    		//Attempts...
    		//file_path << filebuf;
    		//cout << (_T("Directory = {%s}\n"), filebuf) << endl;
    		//sprintf(file_path, "Directory = {%s}\n", filebuf);
    		//_tprintf(_T("Directory = {%s}\n"), filebuf); 
    		//file_path = (_T("Directory = {%s}\n"), filebuf);
    		//sprintf(file_path, "%s", filebuf);
    		cout << TCHARTochar((_T("%s"), filebuf)) << endl;
    
    
    		// return EXIT_SUCCESS;
    	}
    	else
    	{
    		_tprintf(_T("ERROR: cannot get directory\n") );
    		return 0;
    	}
    
    	// Template file, Destination File (Will be over-written), Find what, Replace With. Converts to lCase.
    	doFiles ("c:\\test_file.txt", "c:\\tmp.txt", "C:\\", "F:\\");
    
    	cout << endl << endl << endl << endl << "Executing the webserver..." << endl;
    
    	// Execute Apache Code Needed
    
    	system("pause");
    
    	return 0;
    
    }
    
    
    string replaceString(string &stringToSearch, const string findWhat, string replaceWith)
    {
    	size_t j;
    
    	for ( ; (j = stringToSearch.find( findWhat )) != string::npos ; ) 
    	{
    		stringToSearch.replace( j, findWhat.length(), replaceWith );
    	}
    
    	return stringToSearch;
    }
    
    
    void doFiles(char sourceFile[], char destFile[], char findWhat[], char replaceWith[])
    
    {
    	
    	char str[65535];
    	string newstr;
    	int lineCount;
    
    	ofstream myfile (destFile);
    	myfile << "";
    	lineCount=0;
    
    	cout << "Opening File: " << sourceFile << endl;
    	cout << "Writing File: " << destFile << endl << endl;
    	cout << "Replacing All Instances Of: " << endl << findWhat << endl << endl;
    	cout << "With: " << endl << replaceWith << endl << endl;
    	cout << "Begining Write: "<< endl << endl << endl;
    
    	fstream file_op(sourceFile,ios::in);
    	while(!file_op.eof())
    	{
    	file_op.getline(str,65535);
    
    		//REPLACE STR HERE
    	newstr = replaceString(StringToLower(str),StringToLower(findWhat),StringToLower(replaceWith)).c_str();
    
    	if (myfile.is_open())
    	{
    		lineCount++;
    		myfile << newstr << endl;
    		cout << "Line " << setw(4) << lineCount << ": " << newstr << endl;
    	}
    	else cout << "Unable to open file";
    	}
    		if (myfile.is_open())
    	{
    		myfile.close();
    	}
    	file_op.close();
    	cout << endl;
    
    }
    
    
    string StringToLower(string strToConvert)
    {
       for(unsigned int i=0;i<strToConvert.length();i++)
       {
          strToConvert[i] = tolower(strToConvert[i]);
       }
       return strToConvert;
    }
    
    
    BOOL get_module_directory(TCHAR *obuf, size_t osize)  
    {  
         if ( ! GetModuleFileName(0, obuf, osize) )  
         {  
             *obuf = '\0';// insure it's NUL terminated  
             return FALSE;  
         }  
        
         // run through looking for the *last* slash in this path.  
         // if we find it, NUL it out to truncate the following  
         // filename part.  
        
         TCHAR*lastslash = 0;
        
         for ( ; *obuf; obuf ++)  
         {  
             if ( *obuf == '\\' || *obuf == '/' )  
                 lastslash = obuf;  
         }  
        
         if ( lastslash ) *lastslash = '\0';  
        
         return TRUE;  
    }
    
    
    // My Futile attempt to convert TCHAR to CHAR
    char *TCHARTochar(TCHAR *theTCHAR)
    {
    	char theResult[sizeof(filebuf)];
    	int i;
    
    	for (i= 0; i < sizeof(filebuf); i++)
    	{
    	
    		theResult[i] = theTCHAR[i];
    	}
    
    
    	// EDIT: Fixed it.
    	if (theResult[sizeof(filebuf)] != '\0')
    	{
    		theResult[sizeof(filebuf)+1] << '\0';
    	}
    
    	return theResult;
    
    }
    I tell you what, this is quite a learning curve.
    Last edited by Slyke; Aug 25th, 2008 at 06:13 AM.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Can someone help me with this line of code?

    Also I get this error when trying to run your code:

    PHP Code:
    Error    1    error C2664'GetModuleFileNameW' cannot convert parameter 2 from 'char [512]' to 'LPWCH' 

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Can someone help me with this line of code?

    Actually, forget it all!

    I've figured it out.

    I'm just having troubles with the escape character now.


    Code:
    char *TCHARTocharPath(TCHAR *theTCHAR)
    {
    	int theSize = (sizeof(filebuf));
    	int i;
    	int lastSlash;
    
    	lastSlash=0;
    	*theResult='\0';
    
    	for (i= 0; i <= theSize-1; i++)
    	{
    	
    		theResult[i] = Chr(theTCHAR[i]);
    
    		if (theTCHAR[i]=='\0' || Chr(theTCHAR[i])=='\0')
    		{
    			break;
    		}
    	}
    
    	for (i= 0; i <= theSize-1; i++)
    	{
    		if (theResult[i]=='\\')
    		{
    			theResult[i]=92;
    			i++;
    			theResult[i]=92;
    			i++;
    			theSize+=2;
    		}
    
    		if (theTCHAR[i]==92)
    		{
    			lastSlash=i;
    		}
    	}
    	
    	if (sizeof(theResult)+1 > lastSlash)
    	{
    		theResult[lastSlash]='\0';
    	}
    
    	if (theResult[theSize] != '\0')
    	{
    		theResult[theSize+1] << '\0';
    	}
    
    	//cout << theResult;
    	
    	return theResult;
    
    }
    
    char Chr(int value)
    {
    char a;
    a = value;
    return a;
    }
    I want to make it so that every time there's a \ in theResult, I want another 1 inserted (Before or after doesn't matter) as long as their are 2 of them.

    For some reason I can't get this to work! Have tried just using ascii and putting it in with '\'. Either way when i go to display this:

    Code:
    char *file_path;
    file_path=TCHARTocharPath(filebuf);
    cout << file_path << endl;
    It comes up blank =S. Maybe there's a better way? It has to be left as a char type however.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: [Resolved] Can someone help me with this line of code?

    Thanks for everyone's help =).

    I'll post source code if someone asks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width