You are here:

C++/function declarations/prototypes

Advertisement


Question
Hi David,

1) Still MS Visual C++ 6.0 professional

2) Ok, the last reply you sent was very helpful, and has let me move forward a lot.  But, in getting rid of one of the problems ive come across another.

I wanted to split a record into a structure field, and you suggested changing one of my lines.  I'll just copy the suggestion you made.

3. The line

aUnion.struct1.code = array[4]; //**** error c2440: '=' cannot convert from 'char' to 'char [6]'

Is incorrect. The right-hand-side is a character (the fifth character in array 'array'), while the left-hand-side is an array of six characters. If you are trying to store the contents of 'array' into 'code', you might try something like this:

lstrcpyn(&I_STR.code, array, 6);


Now, when i run the program it all runs fine except for the line you suggested which produces the error:

"C:\Documents and Settings\John\My Documents\Computeach\otago_MODIFIED.cpp(173) : error C2065: 'lstrcpyn' : undeclared identifier
"

My question is what do I have to do to fix this, to declare the function.  To be honest, I had never heard of "lstrcpyn" till you had suggested it, and so I have been doing quite a bit of reading and think I understand the actual function.  Now, if I have been reading correctly, the line you suggested was the actual function, but if I want the function to work I need a function pototype which will tell the compiler how to work the function itself. I have no idea what this is, or how to go about coding it as the books I am trying to learn from are frustratingly vague.

The function would be    lstrcpyn(&I_STR.code, array, 6);

Is making a prototype/declaration the right answer and if so can you suggest how to go about it?  

I dont think you'll need it, but I'll paste the program in below just in case.

thanks for your help

yours,

john


//** Program

  //John Hamilton

   #include <iostream.h>
   #include <fstream.h>
   #include <stdlib.h>
   #include <string.h>
   #include <iomanip.h>
   #include <ctype.h>
  

//define the structures

   struct I_R_RECORD {        
   
  char code[6];  //customer code
  };

   struct D_RECORD {       
  
  char code[6];  //customer code
  
  };

   struct C_RECORD {      
  
     char code[6]; //customer code
  
  };
  
   
  
  


  
   const int MAX = 150;
  
 
     
  
  
  
  int main()
  {



//create an instance of the structures, and a pointer to the structures
  
  struct I_R_RECORD I_STR, *ptr_I;
   struct D_RECORD D_STR, *ptr_D;
   struct C_RECORD C_STR, *ptr_C;



//assign the pointers the structures addresses

  ptr_I = &I_STR;
  ptr_D = &D_STR;
  ptr_C = &C_STR;


   
   

  
//declare input and output files

   ifstream input_file;
  ofstream output_file;
  
   fstream  outFile, prnFile;

  
  
//open input file(test data)
  
  input_file.open("A:532939TD.DAT", ios::in|ios::nocreate);
  if(input_file.fail()) //check for open failure
  {
     cout << endl << "Could not open the file"
         << endl;
     exit(1);
  }



//open output file (validated data)
  
  output_file.open("A:532939VF.DAT", ios::out);
  if(output_file.fail())
  {
     cout << endl << "File not open"
         << endl;
     exit(1);
  }



//open output file (unvalidated data)

  outFile.open("A:532939UV.TXT", ios::out);
  if(outFile.fail())
  {
     cout << endl << "File not open"
         << endl;
     exit(1);
  }



//open printer for output
  
  prnFile.open("lpt1", ios::out);
  
  
  
//Test for file and printer open failure

   if(!input_file.is_open() | !output_file.is_open() | !outFile.is_open() | !prnFile.is_open())
   
  {
     cout << endl << "Could not open the file or printer"
        << endl;
     exit(1);
  }
  

   
//declare arrays

  char  array [MAX];
   char  *array_ptr;
  int num=0, end = 0;
  int total=0;
  int weights[] = { 5,4,3,2},*wptr=weights;      // weights for customer codes
   int pweights[] = { 6,5,4,3,2}, *pwptr=pweights;// weights for part numbers
   


  
  // read data from file to array
 

  while(input_file.peek() !=EOF)
  {
     input_file.getline(array, MAX);
  
  }
  

//modulus checks customer codes

  
  for(num = 0; num < 5; num++)
     array[num] = toupper(array[num]);
   
  
  for(array_ptr = array; *(array_ptr+1); array_ptr++)
  {
     num = *array_ptr - '0';
     num *= *wptr;
     wptr++;
     total += num;
  }

  end = total % 11;
   end = 11 - end;
   
  if ((array[4] -'0') == end)
  {

     lstrcpyn(&I_STR.code, array, 6); //** C:\Documents and Settings\John\My Documents\Computeach\otago_MODIFIED.cpp(173) : error C2065: 'lstrcpyn' : undeclared identifier

       
       
     output_file << I_STR.code  << endl;    
        
  }

  else if ((array[4] -'0') != end)
  {
       outFile << I_STR.code  << endl;          
      
  }
  


         
     


return 0;
  }  

Answer
John, Thank you for your question.

'lstrcpyn' is a standard function in my Standard Edition Visual C++. I'm not sure why your compiler can't find it. It is declared in file winbase.h, as follows:

WINBASEAPI
LPSTR
WINAPI
lstrcpynA(
   LPSTR lpString1,
   LPCSTR lpString2,
   int iMaxLength
   );
WINBASEAPI
LPWSTR
WINAPI
lstrcpynW(
   LPWSTR lpString1,
   LPCWSTR lpString2,
   int iMaxLength
   );
#ifdef UNICODE
#define lstrcpyn  lstrcpynW
#else
#define lstrcpyn  lstrcpynA
#endif // !UNICODE

You might try copying these declarations to your program.

Another possibility is that you could try the renamed function:

LPTSTR StrCpyN(
   LPCTSTR psz1,
   LPCTSTR psz2,
   int cchMax
   );

Another possibility is that you could try the standard C function:

char *strncpy( char *strDest, const char *strSource, size_t count );

By the way, my expression "&I_STR.code" is wrong. Since 'code' is declared as char[6], it is an array. The name of an array is already an address. "&I_STR.code" would be an attempt to take an address of an address, which makes no sense.

David Spector

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


David Spector

Expertise

Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.

Experience

I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Publications
Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

Education/Credentials
ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2016 About.com. All rights reserved.