Jump to content
  • Advertisement
Sign in to follow this  
siliconsurfer

Pointers and Char

This topic is 4819 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have problems with pointers and char's and can't just get my head around what's wrong! I have a function in C++ which reads in data from a file, I then want to compare the data read in, with a char* which is passed into the function. //The function loooks like this getCat(char *filename, char* inName); I declare a variable char *name; Then I read in the data using fgets(buffer, 255, fp); and read the data into the variable by doing sscanf(buffer, "%s", &name); Now I can print name to prove that the data is in there using printf("name = %s\n\n", &name); now I want to compare name with inName so I try if(strcmp(inName, name) == 0){ //do stuff } The problem is it crashes with a segmentation error. Any ideas what I'm doing wrong??

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by siliconsurfer

printf("name = %s\n\n", &name);


One problem is that ampersand before name in the printf. It should be:

printf("name = %s\n\n", name);

otherwise it's trying to print the pointer value itself as a string...

There may be other problems in your function, but that is impossible to tell without the full code...

Tom

Share this post


Link to post
Share on other sites
Well your main problem is that you've misspelt "string".:
void someNameBetterThanGetCatUnlessYouAreActuallyReturningAFeline(std::string filename, std::string expectedName)
{
std::ifstream reader(filename.c_str());
std::string name;
std::getline(reader, name);
if (name == expectedName)
{
// do stuff
}
}

Enigma

Share this post


Link to post
Share on other sites
Here is the complete code. The problem is when I try to do the strcmp on line 34. If I create a variable such as

char *newvar = "anystring";

And compare it with inUrl the code works fine. But comparing inUrl with domain causes the program to crash with a segmentation error. I presume this is to do with how sscanf works, but I can't figure out why, or how to fix it.



int getCat(char *filename, const char *inUrl)
18 {
19 FILE *fp;
20 char buffer[255];
21
22 char *domain;
23 //char *cat;
24
25 if((fp=fopen(filename, "r")) == NULL)
26 {
27 printf("Error opening file %s\n", filename);
28 return -1;
29 }
30 while(!feof(fp))
31 {
32 fgets(buffer, 255, fp);
33 sscanf(buffer, "%s", &domain);
34 if(strcmp(domain, inUrl) == 0){
35 printf("match found\n");
36 break;
37 }
38
39 }
40 fclose(fp);
41
42 return 0;
43 }


Share this post


Link to post
Share on other sites
you declare domain as a pointer to char. You need an array:


char domain[ MAX_SIZE ];
sscanf(buffer, "%s", domain);


Also, if you're doing this in C++, it's preferred to use std::string.

Share this post


Link to post
Share on other sites
if you have ANY trouble AT ALL using char* as a string type you should not be using it. The only time that you should use char* is when you are already quite experienced with std::string and have found one of those rare cases where using char* is better than using the real string type.


#include <string> //note, not string.h
#include <iostream>
#include <fstream>
using namespace std;

void GetFeline(string filename, string expected_data)
{
fstream fin(filename.c_str());
string actual_data;
fin>>actual_data;
if(actual_data==expected_data)
{
//blah blah blah
}
}

Share this post


Link to post
Share on other sites
I'm using char * because I'm working on code, which already uses char * rather than the string data structure.
It's also because I have to return a char * to a calling rpc.

Share this post


Link to post
Share on other sites
need a std::string from a char*

no problem:

std::string str = char_ptr;



need a char* from a string

no problem:

do_something_with_a_char_pointer( str.c_str() );

Share this post


Link to post
Share on other sites
Here's how I would do it.

#define STRING_LENGTH 255
int getCat(const char *filename, const char *inUrl)
18 {
19 FILE *fp;
20 char buffer[STRING_LENGTH];

if (filename == 0 || inUrl == 0)
{
return -1;
}

memset(buffer, 0, (sizeof(char) * STRING_LENGTH));
23 //char *cat;
24
25 if((fp=fopen(filename, "r")) == NULL)
26 {
27 printf("Error opening file %s\n", filename);
28 return -1;
29 }

30 while(!feof(fp))
31 {
32 fgets(buffer, STRING_LENGTH, fp);
34 if(strcmp(buffer, inUrl) == 0)
{
35 printf("match found\n");
36 break;
37 }
39 }
40 fclose(fp);
42 return 0;
43 }



[Edited by - the_dannobot on September 7, 2005 10:34:27 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!