Thread: Invalid name?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    16

    Invalid name?

    This puts out a file with an invaild name. I cant see the problem. Can you help?

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int hednspos();
    int hednepos(int);
    char * hedname(int, int);
    long filesiz(int);
    long fileoff(int);
    char * filecont(long, long);
    int writefile(char *, char *, long);
    
    int main()
    {
    		long size, offset;
    		char *  Data;
    		char * name;
    		char * content;
    		int namespos, nameepos;
    	
    
    	namespos = hednspos();
    	cout << "File Name Start Pos: " << namespos;
    	nameepos = hednepos(namespos);
    	cout << "\nFile Name End Pos: " << nameepos;
    	name = hedname(namespos, nameepos);
    	cout << "\nFile Name: " << name;
    	size = filesiz(namespos);
    	cout << "\nFile Size: " << size;
    	offset = fileoff(namespos);
    	cout << "\nFile Offset: " << offset;
    	content = filecont(offset, size);
    	writefile(content, name, size);
    
        return 0;
    }
    
    int hednspos()
    {
    	
        int  namestart;
        char ptn1;
           
           ifstream hed("datap.hed", ios::in | ios::binary);
    	
            while(ptn1 != 92)
            {
    		hed.read(&ptn1, 1);
            
                 if(ptn1 == 92)
                   {
                      namestart = hed.tellg();
    
    			    }          
    		} 
    		
    		hed.close();
    	 return namestart;
    }
          
    int hednepos(int pnt1)
    {
    	char ch = 92;
    	int nameend;
    	ifstream hed("datap.hed", ios::in | ios::binary);
    	
        	hed.seekg(pnt1);
    			
    			while(ch != 0)
                 {
    		 		hed.read(&ch, 1);
    
             		if(ch == 0)
              		{
               		nameend = hed.tellg();
               		}                              
                 }		
          	
    		hed.close();
    		
          return nameend;
    }
    
    char * hedname(int names, int namee)
    {
    	int size = (names + namee);
    	char ch;
    	char name[size];
    	int x = 0;
    	
    	ifstream hed("datap.hed", ios::in | ios::binary);
    
           hed.seekg(names-1);
    	
    	while(ch != 0)
               {
                   hed.read(&ch, 1);
                   name[x] = ch;
    			   x++;
    			   if(ch == 92)
    			   {
    				   x = 0;
    			   }
               }
    	
          	hed.close();	
          return  name;
     }
     
    long filesiz(int pos)
     {
    	 long size;
    	 
    	ifstream hed("datap.hed", ios::in | ios::binary);
    
        hed.seekg(pos-5);	 
    	hed.read(reinterpret_cast<char*>(&size),4);
    	hed.close();
    	 
    	 return size;
     }
    
    long fileoff(int pos)
     {
    	 long offset;
    	 
    	ifstream hed("datap.hed", ios::in | ios::binary);
    
        hed.seekg(pos-9);	 
    	hed.read(reinterpret_cast<char*>(&offset),4);
    	hed.close();
    	 
    	 return offset;
     }
     
     char * filecont(long offset, long size)
     {
     	char * content;
    	 
    	ifstream hed("datap.wad", ios::in | ios::binary);
    
        hed.seekg(offset);
    	content = new char[size];
    	hed.read(content,size);
    	hed.close();
    	 
    	 return content;
     }
    	 
     int writefile(char * content, char * name, long size)
     {
     
    	 ofstream file(name, ios::out | ios::binary);
    	 
    	 file.write(content, size);
    	 file.close();
    	 
    	return 0;
     }
    It outputs this
    Code:
    File Name Start Pos: 9
    File Name End Pos: 32
    File Name: standardkeyq.bin
    File Size: 2048
    File Offset: 0
    and the name of the file is
    Code:
    ????????????)??????? ???<????﷌????????????????p??? (invalid encoding)
    weird, and yes i know my brackets are out of place, thats just the way my compiler has them.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
    char * hedname(int names, int namee)
    {
    	int size = (names + namee);
    	char ch;
    	char name[size];<<< my compiler doesn't like that line because size is
    not a const
    
    	int x = 0;
    	
    	ifstream hed("datap.hed", ios::in | ios::binary);
    
           hed.seekg(names-1);
    	
    	while(ch != 0)
               {
                   hed.read(&ch, 1);
                   name[x] = ch;
    			   x++;
    			   if(ch == 92)
    			   {
    				   x = 0;
    			   }
               }
    	
          	hed.close();	
          return  name; << illegal return! name disappears as soon as this
    function returns to is caller.
     }
    There are other errors -- you need to pay attention to your compiler's error and warning messages. Treat warnings as errors because they usually are errors.
    Last edited by Ancient Dragon; 03-01-2006 at 07:02 PM.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    16
    Well, what do you suggest i do? I'm freshout of ideas:/

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    The first thing I would do is swap out all of your C-style strings for C++ ones - this should eliminate several problems straight away, and simplify the others.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>Well, what do you suggest i do?

    I already mentioned some things. Main thing: correct all errors and warnings that your compiler displays. For example, your compiler should have screemed at you that variable ch is being used before it is set. So look at that function I posted and see if you can figure out what that means. You declared variable ch, then attempted to use it in a while loop comparing its value to 0. Welllllll.... what is the initial value of that variable? It may, or may not be 0 because the compiler does not initialize it and neither did your program. So the initial value is random making that loop unpredictable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Assignment output help
    By Taka in forum C Programming
    Replies: 13
    Last Post: 09-23-2006, 11:40 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Flood of errors when include .h
    By erik2004 in forum C++ Programming
    Replies: 14
    Last Post: 12-07-2002, 07:37 AM