?
Solved

File Manipulation - Cant Define 'FILE'

Posted on 2007-11-30
20
Medium Priority
?
898 Views
Last Modified: 2008-02-01
I have a compiled DLL that I am trying to use.  Basically I pass it some values, and it passes some back.
That part is working one value at a time.

I need it to be able to run on a file full of values.  I have been out of the C/C++ world for a while so I'm a bit rusty.  My basic problem is I can't open a file.

I am using Visual Studio 2005.  
I have the following includes:
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

When I put the statement:     FILE *fname;
I get the error:   error C2275: 'FILE' : illegal use of this type as an expression

When I try to #include <iostream>  I get ~100 errors coming from the cstdio

#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
 
struct testcase_list
       {
       char      TestNum[6];
       char      TrackStr[20];
       char      RouteStr[11];
       char      ExpectBar[65];
       int       ExpectRC;
       }
void GetTheBarValue( char*);
int main(int argc, char *argv[])
{
	char s[66];
	switch(argc)
	{
		case 3:
			// argv[0] = Program Name
			// argv[1] = All but postnet
			// argv[2] = Postnet
			for (j = 0;j < 20;j++)
			{
				testcase[0].TrackStr[j] = argv[1][j];
			};
			for (j = 0;j < 11;j++)
			{
				testcase[0].RouteStr[j] = argv[2][j];
			};
		case 4:
			argv[0] = argv[0];
			FILE *stream;
	};
 
	GetTheBarValue(s);
	s[65] = '\0';
	printf("Encoded Bar String is: %s\n",s);
}
 
void GetTheBarValue(char* s)
{
    HINSTANCE hinstLib; 
    MYPROC ProcAdd; 
    hinstLib = LoadLibrary(TEXT("usps4cb")); 
    ProcAdd = (MYPROC) GetProcAddress(hinstLib, "USPS4CB"); 
 
   memcpy(TrackString,testcase[0].TrackStr,20);
   TrackString[20] = '\0';
 
   memcpy(RouteString,testcase[0].RouteStr,11);
   RouteString[11] = '\0';
 
   memset(BarString, ' ', sizeof(BarString));   /* Prime blanks!  */
   BarString[65] = '\0';                        /* Set null       */
 
   RetCode = (ProcAdd)(TrackString,RouteString,BarString);
   memcpy(s,BarString,65);
   };

Open in new window

0
Comment
Question by:TeamSrv
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 6
20 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20382145
The include statement

#include <fstream>  
using namespace std;

is missing.

You also should change to

#include <string>  // not <string.h>

Regards, Alex
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20382189
>>>>   case 4:
>>>>       argv[0] = argv[0];
>>>>      FILE *stream;

I assume it is a test only...

>>>>   argv[0] = argv[0];  
makes not much sense

>>>> FILE *stream
Beter use ifstream class and 'stream' is a bade name because of ambiguities.

     ifstream  file(argv[1]);
     if (!file)
     {
            cout << "error " << argv[1] << " cannot be opened for read << endl;
            return;
     }


If you define new variables in a case put all into curly brackets:

case 4:
    {
            ifstream  file(argv[1]);
            ...
           
    }

Regards, Alex
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20382217
>>You also should change to
>>#include <string>  // not <string.h>

why?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20382322
>>> >>#include <string>  // not <string.h>

>>>> why?

std::string is part of the C++ standard. The 'string.h' is old - deprecated - string header which shouldn't be used in newer C++ code.

(I am pretty sure, you know that, Jaime).
0
 
LVL 2

Author Comment

by:TeamSrv
ID: 20382344
Hi Alex.
When I put in:
#include <fstream>  
using namespace std;
I get 100 or so errors coming from cstdio
I have the same problem when I switch string.h to string


Itsmeandnobodyelse:
Yeah I just have the argv[0] = argv[0];   in there so when I comment out the FILE *stream it does not see an empty case statement
I tried adding the ifstream  file(argv[1]); statement, but got the error C2065: 'ifstream' : undeclared identifier
Is there an include that I am missing?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20382383
@alex
string.h belong to the standard C library, like the required by strupr() function. The author is working with C library.
Indeed this is a C language question and your are messing it with your C++ answers.

0
 
LVL 2

Author Comment

by:TeamSrv
ID: 20382403
Yeah this is in both the C and C++ forums.
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 600 total points
ID: 20382406
@TeamSrv
I suggest you to take a decision:
- To continue working with pure C library functions, like FILE *  and strings with char *
- Switch all your code to C++'s STL library, like std::ifstream and std::string

It is not recommended to mix both, specially for a beginner/intermediate programmer
0
 
LVL 2

Author Comment

by:TeamSrv
ID: 20382432
I would love to do that Jamie.  The reason why it is all hacked up like this is I have to use a 3rd party DLL that isnt playing nice.
The only way I was able to get it to work is with:
    HINSTANCE hinstLib;
    MYPROC ProcAdd;
    hinstLib = LoadLibrary(TEXT("usps4cb"));
    ProcAdd = (MYPROC) GetProcAddress(hinstLib, "USPS4CB");
   RetCode = (ProcAdd)(TrackString,RouteString,BarString);

But when I do that in C++, it is not happy.  
Sorry to change questions, but is there a better way to access a function in a DLL using C++?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20382488
in your original question I see:
      FILE *fname;
but in the original source coude I see just:
     FILE *stream;
near to case '4'
Appears to be you are not sending all the source code.

About your last comment, what do you mean when you say that C++ is not happy? which error message are you getting. The more specifc you can be, the faster you will get an accurate answer.

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20382510
>>>> Indeed this is a C language question and your are messing it with your C++
answers.

Don't think that I deserved it to get blamed like that as the author told from "C/C++" in the original question, and used Visual Studio 2005 which mainly is an IDE for C++. Moreover the problem arised when including <iostream> what obviously is a C++ header.

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20382540
>>>> But when I do that in C++, it is not happy.  
>>>> Sorry to change questions, but is there a better way to access a function in a DLL using C++?

You can do in either language.

If your source has .c extension it was compiled with ANSI C compiler. If it has .cpp the C++ compiler was used.

With C++ you have the better support in the Visual Studio.

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20382577
>>Don't think that I deserved it to get blamed like that as the author told from "C/C++" in the original question,
Sorry for that, but not always the TA is a good reference IMHO, I think this new feature is confusing.
But I think the solution has to keep the simpler as possible
0
 
LVL 2

Author Comment

by:TeamSrv
ID: 20382604
@Itsmaeandnobodyelse
I think you are helping me narrow down the issue,  the source code does have the .c extention.  I am re-doing it with the cpp extention.  That has taken care of the file issues that I was having.  Unfortunately it have prevented me from using the dll in the same manor.

@jaime
Errors such as it seems like these are C's way of looking at a dll and arent valid in C++
Ex:  HINSTANCE is being viewed as an undeclared identfier
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 1400 total points
ID: 20382616
The following compiles as a .cpp file

#include <windows.h>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
 
struct testcase_list
       {
       char      TestNum[6];
       char      TrackStr[20];
       char      RouteStr[11];
       char      ExpectBar[65];
       int       ExpectRC;
       };

void GetTheBarValue( char*, testcase_list* );

int main(int argc, char *argv[])
{
      int j;
      testcase_list   testcase[10] = { 0 };
      char s[66];
      switch(argc)
      {
            case 3:
                  // argv[0] = Program Name
                  // argv[1] = All but postnet
                  // argv[2] = Postnet
                  for (j = 0;j < 20;j++)
                  {
                        testcase[0].TrackStr[j] = argv[1][j];
                  };
                  for (j = 0;j < 11;j++)
                  {
                        testcase[0].RouteStr[j] = argv[2][j];
                  };
            case 4:
                  argv[0] = argv[0];
                  FILE *stream;
      };
 
      GetTheBarValue(s, testcase);
      s[65] = '\0';
      printf("Encoded Bar String is: %s\n",s);
}

typedef int (*MYPROC)(char*, char*, char*);
 
void GetTheBarValue(char* s, testcase_list* testcase)
{
    HINSTANCE hinstLib;
    MYPROC ProcAdd;
      char TrackString[20];
      char RouteString[20];
      char BarString[20];
    hinstLib = LoadLibrary(TEXT("usps4cb"));
    ProcAdd = (MYPROC) GetProcAddress(hinstLib, "USPS4CB");
 
   memcpy(TrackString,testcase[0].TrackStr,20);
   TrackString[20] = '\0';
 
   memcpy(RouteString,testcase[0].RouteStr,11);
   RouteString[11] = '\0';
 
   memset(BarString, ' ', sizeof(BarString));   /* Prime blanks!  */
   BarString[65] = '\0';                        /* Set null       */
 
   int RetCode = (ProcAdd)(TrackString,RouteString,BarString);
   memcpy(s,BarString,65);
}
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20382639
>>HINSTANCE is being viewed as an undeclared identfier
this is not related to C or C++, HINSTANCE is defined at windows.h

You can rename your file to .cpp and should work, even using pure C library functions.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20382650
>>>> Sorry for that, ...
No problem.

>>>>But I think the solution has to keep the simpler as possible
I always wonder why C is regarded as simpler than C++. You have less choices in C but IMO it is not simpler.
0
 
LVL 2

Author Comment

by:TeamSrv
ID: 20382730
Thanks for all your help guys!
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20382907
TeamSrv,

I made the prog compile but I didn't look for teh program but only for the syntax errors.

There are some (logical) flaws, which you need to check for, e. g. the final function defines a RetCode but it was not returned. And I fear I have not looked for the correct dimension of the local strings either. As it is C++ now you may consider using string class, e, g, instead

   memcpy(TrackString,testcase[0].TrackStr,20);

you simply can make

          TrackString = testcase[0].TrackStr;

if TrackString was defined as

          string TrackString;   // not  char TrackString[20];
0
 
LVL 2

Author Comment

by:TeamSrv
ID: 20382991
Itsmeandnobodyelse,
Yup I caught those.  The main difficulty was just getting it ported over to C++.
It is functional now, atleast to the point where I can work further with it.
Thanks again
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question