Pointers and Char

Started by
13 comments, last by Enigma 18 years, 7 months ago
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??
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
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
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 }

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.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
You don't allocate memory for domain.

If you're using C++ why are you using char* ?
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    }}
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.
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() );
Here's how I would do it.
#define STRING_LENGTH 255int 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]

This topic is closed to new replies.

Advertisement