Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. passing strings

passing strings

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
23 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J jhwurmbach

    My comments to your code: Why do you get 3 char-arrays as parameters, but never use them? Instead you are putting your read data into local variables (which will get destroyed upon leaving the function). You definitely want more error checking when opening the file and reading the data.


    Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
    George Orwell, "Keep the Aspidistra Flying", Opening words

    D Offline
    D Offline
    dona jain
    wrote on last edited by
    #5

    these mistakes r becoz i was trying something to get solution but i did nt get..i want these local variables to be passed to main function

    J 1 Reply Last reply
    0
    • D dona jain

      Hi all! i need help.. i m calling fileRead function which reads the file and put some values in three strings cName1 ,cSvnPath1 ,cPassword1...now my question is ...that how should i call this function in main file so that these three strings can be utilised in main file only...??? please answer as early as possible. void FileOperation::fileRead(char cName[100] , char cSvnPath[100] , char cPass[100]) { char cName1[100]; char cSvnPath1[100]; char cPassword1[100]; FILE *fp; fp = fopen("config.txt","r"); fgets(cName1 , 100, fp); fgets(cSvnPath1 ,100 , fp); fgets(cPassword1 ,1 ,fp); fclose(fp); }

      R Offline
      R Offline
      Ranjoy Guha
      wrote on last edited by
      #6

      void FileOperation::fileRead(char cName[100] , char cSvnPath[100] , char cPass[100]) { //char cName1[100]; //char cSvnPath1[100]; //char cPassword1[100]; FILE *fp; fp = fopen("config.txt","r"); fgets(cName , 100, fp); fgets(cSvnPath ,100 , fp); fgets(cPass ,100 ,fp); fclose(fp); } Now use the arguments in your main file

      D 1 Reply Last reply
      0
      • D dona jain

        these mistakes r becoz i was trying something to get solution but i did nt get..i want these local variables to be passed to main function

        J Offline
        J Offline
        jhwurmbach
        wrote on last edited by
        #7

        dona jain wrote:

        i want these local variables to be passed to main function

        Why? Wouldn't it be better to pass the buffers from Main to your function? Local variables get destroyed on leaving their scope.


        Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
        George Orwell, "Keep the Aspidistra Flying", Opening words

        D 1 Reply Last reply
        0
        • R Ranjoy Guha

          void FileOperation::fileRead(char cName[100] , char cSvnPath[100] , char cPass[100]) { //char cName1[100]; //char cSvnPath1[100]; //char cPassword1[100]; FILE *fp; fp = fopen("config.txt","r"); fgets(cName , 100, fp); fgets(cSvnPath ,100 , fp); fgets(cPass ,100 ,fp); fclose(fp); } Now use the arguments in your main file

          D Offline
          D Offline
          dona jain
          wrote on last edited by
          #8

          ohh..thanx...can u tell me how to call this function in main FileOpration fcRead; fcRead.fileRead(); this or something else?? fileRead() -->:^) is this correct?

          R C T 3 Replies Last reply
          0
          • J jhwurmbach

            dona jain wrote:

            i want these local variables to be passed to main function

            Why? Wouldn't it be better to pass the buffers from Main to your function? Local variables get destroyed on leaving their scope.


            Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
            George Orwell, "Keep the Aspidistra Flying", Opening words

            D Offline
            D Offline
            dona jain
            wrote on last edited by
            #9

            well this is my requirement to do so..thats y i m here

            J 1 Reply Last reply
            0
            • D dona jain

              ohh..thanx...can u tell me how to call this function in main FileOpration fcRead; fcRead.fileRead(); this or something else?? fileRead() -->:^) is this correct?

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #10

              You just pass the arguments you want to your function. Calling a function and passing parameters is basics C++ so I really suggest you read some book before going on with classes otherwise you'll get totally lost.


              Cédric Moonen Software developer
              Charting control [v1.2]

              1 Reply Last reply
              0
              • D dona jain

                ohh..thanx...can u tell me how to call this function in main FileOpration fcRead; fcRead.fileRead(); this or something else?? fileRead() -->:^) is this correct?

                R Offline
                R Offline
                Ranjoy Guha
                wrote on last edited by
                #11

                char cName[100]; char cSvnPath[100]; char cPass[100]; fileRead(cName, cSvnPath, cPass);

                D 1 Reply Last reply
                0
                • D dona jain

                  ohh..thanx...can u tell me how to call this function in main FileOpration fcRead; fcRead.fileRead(); this or something else?? fileRead() -->:^) is this correct?

                  T Offline
                  T Offline
                  toxcct
                  wrote on last edited by
                  #12

                  dona jain wrote:

                  can u tell me how to call this function in main

                  oh my god !!! :omg: you know what ? stop this immediately, go to a book shop, find Kernigan & Ritchie's Book about the C language, and read (i mean, read and LEARN) !!! then you'll find all this becoming very easy


                  [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                  1 Reply Last reply
                  0
                  • D dona jain

                    well this is my requirement to do so..thats y i m here

                    J Offline
                    J Offline
                    jhwurmbach
                    wrote on last edited by
                    #13

                    Ranjoy Guha already programmed you a function which you simply need to give the buffers as parameters:

                    char buffer1[100];
                    char buffer2[100];
                    char buffer3[100];

                    FileOpration fcRead;
                    fcRead.fileRead( buffer1, buffer2, buffer3);

                    Now look at buffer1, 2, 3 in the debugger. As a side-question: Do you really desire to learn C? And why do you ask your questions in a C++/MFC-Forum? The C++ way of reading a file would be std::ifstream.


                    Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
                    George Orwell, "Keep the Aspidistra Flying", Opening words

                    T 1 Reply Last reply
                    0
                    • R Ranjoy Guha

                      char cName[100]; char cSvnPath[100]; char cPass[100]; fileRead(cName, cSvnPath, cPass);

                      D Offline
                      D Offline
                      dona jain
                      wrote on last edited by
                      #14

                      Hey..its working now...u r realy a nice person here ..others always uggest book..never answer me properly. Thank A lot:-D

                      S T R 3 Replies Last reply
                      0
                      • D dona jain

                        Hey..its working now...u r realy a nice person here ..others always uggest book..never answer me properly. Thank A lot:-D

                        S Offline
                        S Offline
                        sunit5
                        wrote on last edited by
                        #15

                        I think Mr Ranjoy Guha is not the inventor of C++ .Without reading those books even he could not have answered ur problem::laugh:

                        never say die

                        D 1 Reply Last reply
                        0
                        • D dona jain

                          Hey..its working now...u r realy a nice person here ..others always uggest book..never answer me properly. Thank A lot:-D

                          T Offline
                          T Offline
                          toxcct
                          wrote on last edited by
                          #16

                          hey, it's not to annoy you if we suggest you to read books... it's for your own efficiency dude ! it's as if you drive a car without a license... how can you drive on a highway then if you don't learn driving first ? :doh: oh and By the way, if we "never answered properly" as you say, it's because you never ask properly ! :suss: and at last, "u", "y" and "r" are letters; "you", "why" and "are" are the words to use if you want anybody to understand you. you're not an a chat room, and people around here are from all over the world. be compliant with the policies, and everything will be fine. if you don't want to comply, i'll stick on your ass to warn you as you already started to know me for this ;P


                          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                          D 1 Reply Last reply
                          0
                          • J jhwurmbach

                            Ranjoy Guha already programmed you a function which you simply need to give the buffers as parameters:

                            char buffer1[100];
                            char buffer2[100];
                            char buffer3[100];

                            FileOpration fcRead;
                            fcRead.fileRead( buffer1, buffer2, buffer3);

                            Now look at buffer1, 2, 3 in the debugger. As a side-question: Do you really desire to learn C? And why do you ask your questions in a C++/MFC-Forum? The C++ way of reading a file would be std::ifstream.


                            Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
                            George Orwell, "Keep the Aspidistra Flying", Opening words

                            T Offline
                            T Offline
                            toxcct
                            wrote on last edited by
                            #17

                            jhwurmbach wrote:

                            And why do you ask your questions in a C++/MFC-Forum?

                            man, don't be rude on this... there's no C board, and most C++ programmer can understand a C problem. but the problem of the OP is that he doesn't know anything (maybe VB...) but he doesn't want to learn either :doh:


                            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                            1 Reply Last reply
                            0
                            • T toxcct

                              hey, it's not to annoy you if we suggest you to read books... it's for your own efficiency dude ! it's as if you drive a car without a license... how can you drive on a highway then if you don't learn driving first ? :doh: oh and By the way, if we "never answered properly" as you say, it's because you never ask properly ! :suss: and at last, "u", "y" and "r" are letters; "you", "why" and "are" are the words to use if you want anybody to understand you. you're not an a chat room, and people around here are from all over the world. be compliant with the policies, and everything will be fine. if you don't want to comply, i'll stick on your ass to warn you as you already started to know me for this ;P


                              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                              D Offline
                              D Offline
                              dona jain
                              wrote on last edited by
                              #18

                              well i knew this very well that without reading books..i can't...but if its some sort of urgency.. you hav to take someone's help ...is that wrong?...please forget all those past mistakes...and lets be friends.;)

                              T 1 Reply Last reply
                              0
                              • D dona jain

                                Hey..its working now...u r realy a nice person here ..others always uggest book..never answer me properly. Thank A lot:-D

                                R Offline
                                R Offline
                                Ranjoy Guha
                                wrote on last edited by
                                #19

                                Dont Mind... But as Sunit and Tox says you must read some c++ books for your further knowledge.

                                1 Reply Last reply
                                0
                                • S sunit5

                                  I think Mr Ranjoy Guha is not the inventor of C++ .Without reading those books even he could not have answered ur problem::laugh:

                                  never say die

                                  D Offline
                                  D Offline
                                  dona jain
                                  wrote on last edited by
                                  #20

                                  are you an indian?? Me too from India...its seems from your name...JAI HIND

                                  1 Reply Last reply
                                  0
                                  • D dona jain

                                    well i knew this very well that without reading books..i can't...but if its some sort of urgency.. you hav to take someone's help ...is that wrong?...please forget all those past mistakes...and lets be friends.;)

                                    T Offline
                                    T Offline
                                    toxcct
                                    wrote on last edited by
                                    #21

                                    dona jain wrote:

                                    well i knew this very well that without reading books

                                    well, don't get me wrong. i'm not paid to answer on this board. all i do is because i want to and i love it. but i'm still thinking you have some lack of knowledge that you should fix fastly. calling a finction is part of the basics of the C/C++ (and any programming - in general...) languages. if you can't figure out how to call one, then you should start from the beginning.

                                    dona jain wrote:

                                    but if its some sort of urgency

                                    i'm not a doctor ;P

                                    dona jain wrote:

                                    you hav to take someone's help

                                    i'm not obliged to do so, and know that i do my best anyway. but sometimes, i really don't understand the question, so how to help then ? and if i was obliged to help, then the one who's asking should be obliged to ask with all the useful informations we must have to answer well in a one step. Is that Wrong ?

                                    dona jain wrote:

                                    please forget all those past mistakes

                                    i hope you understood me and will make efforts in the future. for my part, i have nothing against you. just against your questions ;) Sincerely... :rose:


                                    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                    1 Reply Last reply
                                    0
                                    • T toxcct
                                      1. i don't understand your question 2) i don't understand your code either 3) no need to say "i need help" (if you ask something, it's obviously because you need help), nor "please answer as early as possible" (people do reply when then know the answer... if no one reply you, it's because no one knows, or no one got your question). so, i ask you the question. What is it you're trying to achieve, and what have you already tried ? what do you mean by "these three strings can be utilised in main file only" ??

                                      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                      D Offline
                                      D Offline
                                      David Crow
                                      wrote on last edited by
                                      #22

                                      toxcct wrote:

                                      if you ask something, it's obviously because you need help

                                      Simple, yet powerful. :)


                                      "A good athlete is the result of a good and worthy opponent." - David Crow

                                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                      1 Reply Last reply
                                      0
                                      • D dona jain

                                        Hi all! i need help.. i m calling fileRead function which reads the file and put some values in three strings cName1 ,cSvnPath1 ,cPassword1...now my question is ...that how should i call this function in main file so that these three strings can be utilised in main file only...??? please answer as early as possible. void FileOperation::fileRead(char cName[100] , char cSvnPath[100] , char cPass[100]) { char cName1[100]; char cSvnPath1[100]; char cPassword1[100]; FILE *fp; fp = fopen("config.txt","r"); fgets(cName1 , 100, fp); fgets(cSvnPath1 ,100 , fp); fgets(cPassword1 ,1 ,fp); fclose(fp); }

                                        J Offline
                                        J Offline
                                        Jim Crafton
                                        wrote on last edited by
                                        #23

                                        Since you're obviously using C++ you should use it correctly. Don't pass crap like char cName[100] as arguments in functions or as variables. Use the STL's std::string class for representing a string. Thus you would rewrite the above as void FileOperation::fileRead( const std::string& cName, const std::string& cSvnPath, const std::string& cPass ) second problem - you have variables defined within the local scope of your function with the same name as the arguments you've defined in your function. That could be problematic. You should go out and get a good book on C++ first and get comfortable with the basics.

                                        ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups