Like Tree2Likes
  • 2 Post By nvoigt

Help me with this error plz

This is a discussion on Help me with this error plz within the C++ Programming forums, part of the General Programming Boards category; I m writing some statements to with str.find function i am using a pointer with it error : ...File Compression\File ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    82

    Help me with this error plz

    I m writing some statements to with str.find function
    i am using a pointer with it

    error :
    ...File Compression\File Compression\main.cpp|46|error: request for member 'c_str' in 'filename_p', which is of non-class type 'char*'|
    Code:
    else if (((*filename_p).find(".txt")) != string::npos)
            {...
    char *filename_p;
        filename_p=new char [300];
    ....
                chck_name=1;
            }

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    You did some very wierd things... you are using filename_p even before it is defined and you are using a char* as if it were a string*, which it isn't. You need to post a complete example of what you want to do.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    82
    I have a code
    i declared a pointer filename_p at line 18, nw i want to do that if th user inputs any other extension then .txt then the program must continue. for that i write some statements from line 46 to 53..

    but i knw the fidn function dnt work with pointer but i have initialized it t a charachter array's llocation, thats [300]

    if i m wrong please correct me, and help me with the solution

    Code:
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include <string>
    
    using namespace std;
    
    // Function protos
    void init_array(char ascii[63], char myb[64]);
    
    // !! Function protos
    
    int main()
    {
    //declaration & init
        char ascii[63];
        char myb[64];
        char *filename_p;
        filename_p=new char [300];
        int a=48;
        int b=0;
        int count_filename=0;
        int count_orig=0;
        int chck_name=0;
        ifstream in_orig;
        ofstream test; // for Testing
    // !! Declaration
    // Input fiile name and open
        test.open("test.txt");
        init_array(ascii,myb);
        test << ascii;
        do
        {
            cout << "Enter a file name or complete path(if outside the current folder) or 0 to exit:" << endl;
            cin.getline(filename_p,300);
            in_orig.open(filename_p);
            if (*filename_p == '0')
            {
                exit(1);
            }
           else if (in_orig.fail())
            {
                cout << "File doesnot exist at the current location."<< endl;
            }
    
            else if (((*filename_p).find(".txt")) != string::npos)
            {
                chck_name=1;
            }
    
    
        }
        while ((in_orig.fail()) && chck_name);
    // !! filename and open
    
    
    
    //Counting no of char in filename
        a=0;
        cin.ignore(120,'\n');
        while (filename_p[a]!='\0')
        {
            count_filename++ ;
            cout<< a << endl;
            a++;
        }
    
        cout << count_filename;
    // !Counting no of char in filename
    
    
        return 0;
    }
    
    
    // Functions to Initialize arrays
    void init_array(char ascii[63], char myb[64])
    {
        // ofstream test1;
        //test1.open("test1.txt");
        int a=48;
        int b=0;
    
        /* storing values in ascii, i.e
        ={0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z, }*/
        a=48;
        while (a < 123)
        {
            if (((a >= 48) && (a < 58)) || ((a >= 65) && (a <= 90)) || ((a >= 97) && (a <= 122)))
            {
                ascii[b]=a;
                b++;
            }
            if ( b==63)
            {
                ascii[b]=32;
            }
            a++;
    
    
        }
    // !! Stored in ascii
        //cout  << ascii << endl;
        a=0;
        while (a < 63)
        {
            myb[a]=char(a);
            /*char c;
            c=myb[a]| (char)65;
    
            //test1 << a << " " << (int)myb[a] << endl;*/
            (int)a++;
        }
        /*char c=myb[1];
        c = c << 6;
        c= c| char(1);
         test1 << c << endl;
        */
    }

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    I'm not sure why you are using a char* at all. If you like std::string and it's find function, use it. Maybe you should start fresh with a clean file and do one thing only until it works. Read a string and print it. Then check if the user entered zero. Then open a file with the string the user entered. Don't try all at once.

    but i knw the fidn function dnt work with pointer but i have initialized it t a charachter array's llocation, thats [300]
    I'd also like to point out that we're here to help with programming, not with spelling. Please make sure we can read your post without problems.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,584
    and don't private message forum members when you don't get the answers you like.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    82
    Well i have created a char pointer, which is similar to char array.
    char array performs all functions of string library
    but my char pointer 'filename_p' is not performing find function, help me plzz

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,259
    Quote Originally Posted by fredsilvester93
    char array performs all functions of string library
    No, it does not. A char array can store a null terminated string, but the library facilities used to deal with null terminated strings are different from those used to deal with std::string objects. Just use a std::string object. When you need to open the file, use the std::string object's c_str() member function.
    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

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    As nvoigt said, find works with std::string, not char[ ] or char*

    To work with a C-style char array or pointer, you need say strstr.
    else if ( strstr(filename_p,".txt") != NULL)

    But then you should also decide whether you want to program in C OR C++, because this mix-and-match style isn't good for learning either language in the long run.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    82
    Now i m not getting how to open the file using input string
    here filename is string

    Code:
    do
        {
            cout << "Enter a file name or complete path(if outside the current folder) or 0 to exit:" << endl;
            getline(cin,filename);
            in_orig.open(filename.c_str);
            if (filename == '0')
            {
                exit(1);
            }
            else if (in_orig.fail())
            {
                cout << "File doesnot exist at the current location."<< endl;
            }
    
            else if (filename.find(".txt") != string::npos)
            {
                chck_name=1;
            }
    
    
        }
        while ((in_orig.fail()) && chck_name);

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Yeah, there's an error in your code. But your compiler already told you so, so I will not bore you with the details.

    Maybe you should start fresh with a clean file and do one thing only until it works. Read a string and print it. Then check if the user entered zero. Then open a file with the string the user entered. Don't try all at once.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  2. Replies: 4
    Last Post: 07-24-2011, 10:38 PM
  3. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  4. Replies: 3
    Last Post: 10-02-2007, 10:12 PM
  5. Replies: 1
    Last Post: 01-11-2007, 05:22 PM

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