Error cannot convert `FILE*' to `const char*'

on the home streach one more please

Page 1 of 1

7 Replies - 13485 Views - Last Post: 06 October 2006 - 04:09 PM Rate Topic: -----

#1 mitchelltab  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 13-July 06

Error cannot convert `FILE*' to `const char*'

Post icon  Posted 04 October 2006 - 08:10 PM

need help with this error not to sure what this error means..

Quote

72 C:\Dev-Cpp\tmitchellwk8.cpp cannot convert `FILE*' to `const char*' for argument `1' to `int printf(const char*, ...)'
77 C:\Dev-Cpp\tmitchellwk8.cpp expected `}' at end of input
77 C:\Dev-Cpp\tmitchellwk8.cpp expected `}' at end of input


  #include <stdio.h>
#include <string.h>
#include <stdlib.h>


#define FLASE 0
#define TRUE 1
#define Nlen 25

typedef struct person{
	 char FName [Nlen +1];
	 char LName [Nlen +1];
	 char Sex;
	 char Grade;
	 struct person *Next;
} PERSON;

void PrintList (PERSON*);
int DeletePerson;
PERSON* InsertNode (PERSON*, PERSON*);	 

main ()
{
	PERSON *STU; //TEmp pointer used to creat list
	int Ans;	//User responce
	int Len,	  //Name Length
		Deleted; //True if requested item deleted
	char FName [Nlen +1];  // first name
	char LName [Nlen +1];  // lase name
	char Grade, Sex, Ans1; 
	FILE *fin;
	 fin = fopen( "data.dat", "rw");
	 
PERSON *TOP = NULL; //Initialize top list point to Null	 
	
	 
 while (fscanf(fin, "%25[^ ]. %25[^ ].%c %c\n", Nlen, STU->LName, STU->FName, STU->Sex,STU->Grade) != EOF){
	 
	  printf ("chose option you want to do\n");
	  printf ("Option 1 : Enter New Recode");
	  printf ("Option 2 : Display List");
	  scanf ("%d", Ans);
	  
	   switch (Ans){
		  case '1' :	   
			 STU = (PERSON*) malloc (sizeof (PERSON));
			  if (STU == NULL)
				printf ("Error -- could not allocate memory\n\n");
			  else {
				 printf ("\nEnter students first name ( uo to %d letters):", Nlen);
				 fgets (STU->FName, Nlen+1, stdin);
				 Len = strlen (STU->FName);
				   if (STU->FName[Len-1]== '\n')
					   STU->FName [Len-1] == '\0';
				  
				  printf ("\nEnter students first name ( uo to %d letters):", Nlen);
				 fgets (STU->LName, Nlen+1, stdin);
				 Len = strlen (STU->LName);
				   if (STU->LName[Len-1]== '\n')
					   STU->LName [Len-1] = '\0';  
				   
				 printf ("Please enter Sex");
				 scanf ("%c", &STU->Sex);
				 printf ("Please enter Grade");
				 scanf ("%c", &STU->Grade);  
				 STU->Next= NULL; 
				  break;		 
				 } //else statment end
						 
			 
			case '2' :
				printf (fin, "%s %s %c %c\n", Nlen, STU->LName, STU->FName, STU->Sex,STU->Grade);
				  break;		
 getchar ();
 getchar ();
 return 0;
}




edit: modified title to be more descriptive ~ jayman9

Is This A Good Question/Topic? 0
  • +

Replies To: Error cannot convert `FILE*' to `const char*'

#2 Antiokus  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 192
  • Joined: 06-September 06

Re: Error cannot convert `FILE*' to `const char*'

Posted 04 October 2006 - 09:20 PM

Okay, well at the bottom of the code you are missing two closing brackets
			case '2' :
				printf (fin, "%s %s %c %c\n", Nlen, STU->LName, STU->FName, STU->Sex,STU->Grade);
				  break; 
		 }
	 }	   
getchar ();
getchar ();
return 0;
}



that will get rid of most of the errors up there. Also in your errors, its saying you saved the file as a CPP file. Is that correct because that needs to be changed too.
Was This Post Helpful? 0
  • +
  • -

#3 Antiokus  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 192
  • Joined: 06-September 06

Re: Error cannot convert `FILE*' to `const char*'

Posted 04 October 2006 - 09:26 PM

Just as a standard, you may also want to include a "default" statement on your switch, it acts as a preventative against wrong input and program crashes because of mistakes:
default:
	printf("Wrong choice, please re-enter");
	break;


or something to that effect.
Was This Post Helpful? 0
  • +
  • -

#4 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Error cannot convert `FILE*' to `const char*'

Posted 04 October 2006 - 11:39 PM

Mitchell,

The problem is not as hard as it looks.

The compiler gave you a clue where to look.

 72 C:\Dev-Cpp\tmitchellwk8.cpp cannot convert `FILE*' to `const char*' for argument `1' to `int printf(const char*, ...)'
77 C:\Dev-Cpp\tmitchellwk8.cpp expected `}' at end of input
77 C:\Dev-Cpp\tmitchellwk8.cpp expected `}' at end of input 


Lets break that down...

72 C:\Dev-Cpp\tmitchellwk8.cpp "message" consists of

72 = line number in your code
C:\Dev-Cpp\tmitchellwk8.cpp = file where the problem occured
"message" = while often very short, can tell you what is wrong

in your case, line 72 is the following code
printf (fin, "%s %s %c %c\n", Nlen, STU->LName, STU->FName, STU->Sex,STU->Grade);


The message tells me that you are trying to use the variable fin which is a pointer to a FILE, and not a pointer to a char.

"printf" cannot be used the way you are trying to use it. Look up help on writing to files. "printf" is the old stdio way to "print FORMATTED"...

hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#5 mitchelltab  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 13-July 06

Re: Error cannot convert `FILE*' to `const char*'

Post icon  Posted 05 October 2006 - 06:47 AM

There are no error from the compiler but When i go to case 1 it enter in the data it fails any ideas.



#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#define FLASE 0
#define TRUE 1
#define Nlen 25

typedef struct person{
	 char FName [Nlen +1];
	 char LName [Nlen +1];
	 char Sex;
	 char Grade;
	 struct person *Next;
} PERSON;

void PrintList (PERSON*);
int DeletePerson;
PERSON* InsertNode (PERSON*, PERSON*);	 

main ()
{
	PERSON *STU; //TEmp pointer used to creat list
	int Ans;	//User responce
	int Len,	  //Name Length
		Deleted; //True if requested item deleted
	char FName [Nlen +1];  // first name
	char LName [Nlen +1];  // lase name
	char Grade, Sex, Ans1; 
	FILE *fin;
	 fin = fopen( "e:data.dat", "rw");
	 
PERSON *TOP = NULL; //Initialize top list point to Null	 
	
	 
 while (Ans != 3)
 {
	  fscanf(fin, "%25[^ ]. %25[^ ].%c %c\n", Nlen, STU->LName, STU->FName, STU->Sex,STU->Grade);
	  printf ("chose option you want to do\n");
	  printf ("Option 1 : Enter New Recode");
	  printf ("Option 2 : Display List");
	  scanf ("%d", Ans);
	  
	   switch (Ans){
		  case '1' :	   
			 STU = (PERSON*) malloc (sizeof (PERSON));
			  if (STU == NULL)
				printf ("Error -- could not allocate memory\n\n");
			  else {
				 printf ("\nEnter students first name ( uo to %d letters):", Nlen);
				 fgets (STU->FName, Nlen+1, stdin);
				 Len = strlen (STU->FName);
				   if (STU->FName[Len-1]== '\n')
					   STU->FName [Len-1] == '\0';
				  
				  printf ("\nEnter students first name ( uo to %d letters):", Nlen);
				 fgets (STU->LName, Nlen+1, stdin);
				 Len = strlen (STU->LName);
				   if (STU->LName[Len-1]== '\n')
					   STU->LName [Len-1] = '\0';  
				   
				 printf ("Please enter Sex");
				 scanf ("%c", &STU->Sex);
				 printf ("Please enter Grade");
				 scanf ("%c", &STU->Grade);  
				 STU->Next= NULL; 
				  break;		 
				 } //else statment end
						 
			 
			case '2' :
				 fscanf(fin, "%25[^ ]. %25[^ ].%c %c\n", Nlen, STU->LName, STU->FName, STU->Sex,STU->Grade);
				 printf ("%s %s %c %c\n", LName, FName, Sex, Grade);
				  break;		
				  
				  }
				  }
 getchar ();
 getchar ();
 return 0;
}
 

This post has been edited by mitchelltab: 05 October 2006 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

#6 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Error cannot convert `FILE*' to `const char*'

Posted 05 October 2006 - 05:56 PM

View Postmitchelltab, on 5 Oct, 2006 - 06:47 AM, said:

There are no error from the compiler but When i go to case 1 it enter in the data it fails any ideas.


You don't explain when you get a failure, so anything I say is a "stab in the dark".

On examining the code (trimmed down to the case 1) I noticed you do this :

View Postmitchelltab, on 5 Oct, 2006 - 06:47 AM, said:

scanf ("%c", &STU->Sex);


The use of the "&" means you are sending a reference to the structure STU and not the string the scanf is expecting. Its possible the compiler simply does see that as a compile time error.

<snip>   
STU = (PERSON*) malloc (sizeof (PERSON));
if (STU == NULL)
	printf ("Error -- could not allocate memory\n\n");
else {
	printf ("\nEnter students first name ( uo to %d letters):", Nlen);
	fgets (STU->FName, Nlen+1, stdin);
	Len = strlen (STU->FName);
	if (STU->FName[Len-1]== '\n')
		STU->FName [Len-1] == '\0';
				  
	printf ("\nEnter students first name ( uo to %d letters):", Nlen);
	fgets (STU->LName, Nlen+1, stdin);
	Len = strlen (STU->LName);
	if (STU->LName[Len-1]== '\n')
	STU->LName [Len-1] = '\0';  
				   
	printf ("Please enter Sex");
	scanf ("%c", &STU->Sex);	// <= possible assignment problem
	printf ("Please enter Grade");
	scanf ("%c", &STU->Grade); // <= possible assignment problem 
	STU->Next= NULL; 
	break;		 
} //else statment end
	<snip>
 

This post has been edited by gregoryH: 05 October 2006 - 05:57 PM

Was This Post Helpful? 0
  • +
  • -

#7 mitchelltab  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 13-July 06

Re: Error cannot convert `FILE*' to `const char*'

Post icon  Posted 06 October 2006 - 07:04 AM

ok know i took out the &
so that i would go into the struct But im still having an isses to get into my while loop i changed the while statement. I have put linked list in there. Need help to get it to work as well
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>


#define FLASE 0
#define TRUE 1
#define Nlen 25

typedef struct person{
	 char FName [Nlen +1];
	 char LName [Nlen +1];
	 char Sex;
	 char Grade;
	 struct person *Next;
} PERSON;


struct listNode
{
	   char data;
	   struct listNode *priorPtr;
	   struct listNode *nextPtr;
	   } ListNode;
	   
	   struct listNode *start = NULL;
	   struct listNode *last = NULL;
	   
	   
void PrintList (PERSON*);
int DeletePerson;
PERSON* InsertNode (PERSON*, PERSON*);	 


void insert( struct listNode   *newnode,  /* new node */
				struct listNode   **start, /* first node */
				struct listNode   **last /* last node */
				)
{
	struct listNode  *oldptr, *ptr;
	if(*last == NULL) /* first node in linklist */
	{
		newnode->nextPtr = NULL;
		newnode->priorPtr = NULL;
		*last = newnode;
		*start = newnode;
		return;
	}
	ptr = *start; /* start at top of linklist */
	oldptr = NULL;

	while(ptr)
	{
		if(ptr->data < newnode->data )
		{
			oldptr = ptr;
			ptr = ptr->nextPtr;
		}
		else
		{
			if(ptr->priorPtr)
			{
				ptr->priorPtr->nextPtr = newnode;
				newnode->nextPtr = ptr;
				newnode->priorPtr = ptr->priorPtr;
				ptr->priorPtr = newnode;
				return;
			}
			newnode->nextPtr = ptr; /* newnode first node */
			newnode->priorPtr = NULL;
			ptr->priorPtr = newnode;
			*start = newnode;
			return;
		}
	}
	oldptr->nextPtr = newnode;
	newnode->nextPtr = NULL;
	newnode->priorPtr = oldptr;
	*last = newnode;
}
main ()
{
	PERSON *STU; //TEmp pointer used to creat list
	int Ans;	//User responce
	int Len,	  //Name Length
		Deleted; //True if requested item deleted
	char FName [Nlen +1];  // first name
	char LName [Nlen +1];  // lase name
	char Grade, Sex, Ans1; 
   	struct listNode  *start = NULL;		 
	struct listNode  *last = NULL;	
	struct listNode  *data = NULL;
	struct listNode  *tempnode  = NULL;   
	srand( (unsigned)time( NULL ) );
	FILE *fin;
	
	 
PERSON *TOP = NULL; //Initialize top list point to Null	 
 fin = fopen( "e:data.txt", "rw");	
	 
 while (Ans!= 3)
 {
	  fscanf(fin, "%25s %25s %c %c\n", STU->LName, STU->FName, STU->Sex,STU->Grade);
	  printf ("chose option you want to do\n");
	  printf ("Option 1 : Enter New Recode\n");
	  printf ("Option 2 : Display List\n");
	  printf ("Option 3 : Quit\n");
	  scanf ("%d", Ans);
	  
	   switch (Ans)
	   {
		  case '1' :	   
		  STU = (PERSON*) malloc (sizeof (PERSON));
if (STU == NULL)
	printf ("Error -- could not allocate memory\n\n");
else {
	printf ("\nEnter students first name ( uo to %d letters):", Nlen);
	fgets (STU->FName, Nlen+1, stdin);
	Len = strlen (STU->FName);
	if (STU->FName[Len-1]== '\n')
		STU->FName [Len-1] == '\0';
				  
	printf ("\nEnter students first name ( uo to %d letters):", Nlen);
	fgets (STU->LName, Nlen+1, stdin);
	Len = strlen (STU->LName);
	if (STU->LName[Len-1]== '\n')
	STU->LName [Len-1] = '\0';  
				   
	printf ("Please enter Sex");
	scanf ("%c", STU->Sex);	// <= possible assignment problem
	printf ("Please enter Grade");
	scanf ("%c", STU->Grade); // <= possible assignment problem 
	STU->Next= NULL; 
	break;		 
} //else statment end		 
			  
						 
			 
			case '2' :
				 fscanf(fin, "%25s %25s %c %c\n", STU->LName, STU->FName, STU->Sex, STU->Grade);
				 printf ("%s %s %c %c\n", LName, FName, Sex, Grade);
				 break; 
						
			  case '3' :  
				  printf ("you chose to exit.  Hit enter to close");  
				  break; 
			}//end of switch
	  } // end of while
	  //or (LName = )
//
	//ata = (struct listNode  *) malloc(sizeof(listNode));
	//f(!data)
	//return -1;
	//ata->data=  Stu->LName;
	//nsert(data, &start, &last);
 getchar ();
 getchar ();
 return 0;
}





Was This Post Helpful? 0
  • +
  • -

#8 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Error cannot convert `FILE*' to `const char*'

Posted 06 October 2006 - 04:09 PM

View Postmitchelltab, on 6 Oct, 2006 - 07:04 AM, said:

But im still having an isses to get into my while loop i changed the while statement. I have put linked list in there. Need help to get it to work as well


Looking at your code, there would be benefit in making the main() while loop smaller by placing your code in functions to get some readability.

Also, you have been creating elements of the type listNode instead of accessing the internal data.

I think you may have a typo here
#define FLASE 0


"FLASE" will not match "FALSE".

In your main(), there is an issue here.
   	struct listNode  *start = NULL;		 
	struct listNode  *last = NULL;	   
	struct listNode  *data = NULL;
	struct listNode  *tempnode  = NULL;	  


I have reason to believe that you actually wanted to work with the internal data and pointers. 2 things:

a) listNode * start doesn't create the structure in memory. You must use malloc to create a copy of the struct and free deallocate when you are closing your program to prevent memory leak

B) struct listNode * data won't access the data element inside the structure, you must do that this way:
struct listNode * start = malloc ( sizeof ( listNode ) );
// if malloc fails, start will be a NULL
// I will assume this won't happen
start->data = NULL;
// etc

// near the end of the program, you must free the memory
// remember this applies to data too
free ( start->data );
free ( start ); //



Accessing PERSON in a linked list requires a little nesting to get to the element
start->data = malloc ( sizeof ( PERSON ) );
cout << "Enter Name :";
string name;
cin >> name;
strcpy ( start->data->FName , name );

You must think about this carefully, as improper malloc (or lack of) and forgetting to free are common causes for linked list failures.

Also - I just spotted that you want to work with PERSON, not a char... listNode will need data to be of type PERSON, not a char.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1