strcmp issue

This is a discussion on strcmp issue within the C++ Programming forums, part of the General Programming Boards category; idk why but i cannot figure out what is wrong with my program. i found it crashes on strcmp but ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    43

    strcmp issue

    idk why but i cannot figure out what is wrong with my program. i found it crashes on strcmp but as far as i can tell this is how i have always used it. i'll post what i think are the impotent parts.

    Code:
            FILE* fin;							//file pointer
    	fin = fopen("profiles.data", "r");	//open file
    
    	if(fin == NULL)
    	{
    		printf("ERROR opening file\n");
    		return -1;
    	}
    
    	char* input = "";					//input form file.
    	fscanf(fin, "%[^\n]", &input);		//read line
    
    	//initialize data liked list
    	initializeData(); //<----dont think this matters it never touches input or anything else
    
    	printf("%s ?= **\n", &input); //<----this works
    
    	//look for start of file. start of file will contain "**" on a single line by it self.
    	while(strcmp(input, "**") != 0) //<---crashes here
    	{
    		fscanf(fin, "%[^\n]", &input);
    	}
    
    //more to do after this...
    EDIT fixed =! to !=. this was right in my code just wrong here.
    Last edited by t014y; 05-20-2011 at 12:14 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    =! should probably be !=, but I would expect you to get a compile error rather than a runtime crash.

    By the way, is this supposed to be C or C++?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User xentaka's Avatar
    Join Date
    May 2011
    Posts
    60
    EDIT: Clarity.

    Beginner here so actual question:

    So what happens when you enter the while loop and there are no more "**" in the file and you reach the EOF?
    Last edited by xentaka; 05-20-2011 at 12:04 PM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,550
    Where do you allocate space for the variable input? (I mean what it points to)
    I see you declare it as a pointer to char; I just see no place you allocate enough space to hold 3 bytes of data pointed to by it.

    Tim S.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    laserlight

    =! should probably be !=, but I would expect you to get a compile error rather than a runtime crash.

    By the way, is this supposed to be C or C++?
    in my code is it "!=" idk why it is wrong here. and this is C++ code.

    Beginner here so actual question:

    So what happens when you enter the while loop and there are no more "**" in the file and you reach the EOF?
    the file is set up so that the ** indicates the start of the data in the file. if there is no ** then the data file is set up wrong and seeing as i set up the data file it would fix that.

    stahta01

    Where do you allocate space for the variable input? (I mean what it points to)
    I see you declare it as a pointer to char; I just see no place you allocate enough space to hold 3 bytes of data pointed to by it.

    Tim S.
    i do not recall needing to allocate space for scanf or fscanf. how ever i did find it strange that i had a compiler error when i tried to read in the strings with out the '&'. i was under the impression that strings were already pointers and the variable didn't need the '&'. and the printf works fine with the '&'

    this is also my first time really programming in C++ so basic differences may not be know to me.

    EDIT: more info
    Last edited by t014y; 05-20-2011 at 12:13 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by t014y
    this is C++ code.
    Is there any specific reason that you are using C-style I/O over C++-style I/O? Note that you can work with std::string objects instead of dealing with null terminated strings.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    Is there any specific reason that you are using C-style I/O over C++-style I/O? Note that you can work with std::string objects instead of dealing with null terminated strings.
    the reason is simply because I'm used to C-stile I/O. i didn't think it would make a difference.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by t014y View Post
    i do not recall needing to allocate space for scanf or fscanf. how ever i did find it strange that i had a compiler error when i tried to read in the strings with out the '&'. i was under the impression that strings were already pointers and the variable didn't need the '&'. and the printf works fine with the '&'EDIT: more info
    You do need to allocate the memory. You pass a pointer to pointer, instead of pointer to buffer, which results (probably, there might be another bug) in crash. Additionaly, you cast away the const-correctness when assigning a string literal to a char* variable. (the literal has type const char*)

    Code:
    char input[256];
    fscanf(fin, "%s", input);		// I changed it to %s; I dont even know what this C-style rubbish meant
    I never put signature, but I decided to make an exception.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    i changed it to be more C++ like and it seems to work now. still dont know why it didn't work before. the %[^\n] reads in a whole line where %s reads in a string that is the reason i was using that.

    now that section of code looks like this...

    Code:
    	ifstream fin;							//file pointer
    	fin.open("profiles.data");				//open file
    
    	if(!fin.is_open())
    	{
    		printf("ERROR opening file\n");
    		return -1;
    	}
    
    	string input = "";						//input form file.
    
    	getline(fin, input);
    
    	//initialize data liked list
    	initializeData();
    
    	//look for start of file. start of file will contain "**" on a single line by it self.
    	while(input.compare("**") != 0)
    	{
    		cout << input << "\n";
    		//fscanf(fin, "%[^\n]", &input);
    		getline(fin, input);
    	}
    thanx for the help. if you see any issues that might come up from this plz let me know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  2. Help: strcmp
    By LuizCPFL in forum C Programming
    Replies: 5
    Last Post: 12-11-2008, 02:09 AM
  3. What does strcmp actually do?
    By Brewer in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 12:32 PM
  4. strcmp
    By leebean in forum C++ Programming
    Replies: 6
    Last Post: 04-22-2003, 05:53 PM
  5. strcmp
    By stanleyw in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 06:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21