LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-21-2011, 05:59 AM   #1
naren123.k
Member
 
Registered: Jan 2011
Posts: 85

Rep: Reputation: 17
type conversion problem


Hi all,
i am having following errors can any one tell me how to solve this

SELF/self.cc: In member function ‘void SELF::rt_print(nsaddr_t)’:
SELF/self.cc:378: error: invalid conversion from ‘char’ to ‘const char*’
SELF/self.cc:378: error: initializing argument 2 of ‘FILE* fopen(const char*, const char*)’
SELF/self.cc:386: warning: format ‘%i’ expects type ‘int’, but argument 9 has type ‘double’
SELF/self.cc:386: warning: format ‘%.4lf’ expects type ‘double’, but argument 10 has type ‘int’
SELF/self.cc:386: warning: too few arguments for format


i am getting this for below function
void
SELF::rt_print(nsaddr_t node_id) {
FILE * dumpFile;
char dumpFileName[50] = "rtable.txt";

dumpFile = fopen(dumpFileName, 'a');

self_rt_entry *rt;

fprintf(dumpFile, "=======================================================");

for (rt=rtable.head();rt; rt = rt->rt_link.le_next) {

fprintf(dumpFile, "NODE: %it %.4lft %it %it %it %it %it %.4lft %d n", node_id, CURRENT_TIME, rt->rt_dst, rt->rt_nexthop, rt->rt_hops, rt->rt_seqno, rt->rt_expire, rt->rt_flags);

}

fclose(dumpFile);
}

so plz tell me the solution...
 
Old 04-21-2011, 09:12 AM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
How are we supposed to know which line is which?

Also, use [code] blocks.
 
Old 04-21-2011, 11:29 AM   #3
winning
Member
 
Registered: Apr 2011
Posts: 70

Rep: Reputation: 13
Hello. Your compiler is fairly clear in telling you what you are doing wrong.

Code:
FILE *fopen(const char *path, const char *mode);
The second parameter is a pointer to const char. Your invocation is:

Code:
fopen(dumpFileName, 'a');
The second argument is 'a', which is a character literal, which in C++ has type char. You could try this instead:

Code:
fopen(dumpFileName, "a");
"a" is a string literal, which has the appropriate type. Compiler warnings are important in general and in many cases should be seen as error indicators. Your compiler is letting you know that your call to fprintf will most likely not do what you want it to do. I'd suggest breaking it into more (manageable) calls to fprintf.

Having said that, why are you using C file I/O functions instead of C++ streams? I'm kind of guessing the answer, and it has something to do with the fact that you are writing object-based programs despite not being able to correct an error as simple as the one pointed out by your compiler.

Some further observations:

1. At best, the declaration of dumpFileName is odd. You are not modifying it after it has been created and thus:
  • There is no need to specify the number of elements. Your compiler will figure this out. The following would not only be enough but also superior at least from a memory-usage point of view: char dumpFileName[] = "...".
  • Express the fact that you have no intention of modifying the contents of dumpFileName through code: const char dumpFileName[] = "..."

2. You are not checking the return value of fopen (and I must assume that this is a general trend throughout your code). Most likely you should be doing it.

Last edited by winning; 04-21-2011 at 11:42 AM.
 
Old 04-22-2011, 02:17 AM   #4
naren123.k
Member
 
Registered: Jan 2011
Posts: 85

Original Poster
Rep: Reputation: 17
thank you its been solved.
 
Old 04-26-2011, 03:32 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,635

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by naren123.k View Post
thank you its been solved.
Great.

Since you came to the community for help, how about posting the solution here, so the community can benefit?
 
Old 04-26-2011, 03:41 PM   #6
winning
Member
 
Registered: Apr 2011
Posts: 70

Rep: Reputation: 13
Quote:
Originally Posted by TB0ne View Post
Great.

Since you came to the community for help, how about posting the solution here, so the community can benefit?
He does not need to do that since the solution is presented in my previous post.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
type conversion??? iqra Programming 14 07-02-2010 02:59 PM
Type conversion problem- C++ vivekr Programming 7 01-02-2006 10:10 AM
Data type conversion in C zaichik Programming 6 09-10-2005 05:47 PM
c + type conversion dilberim82 Programming 8 03-08-2005 12:17 PM
a question on type conversion in C++ ReverseLogic Programming 3 11-27-2001 11:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration