CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2009
    Posts
    2

    Importing Variables From Outside File

    I'm looking to be able to import variables from an outside source. If i were to create a file that said:
    var1 = 15
    var2=C:/Files/run.exe
    How would i accomplish this? What would be the best alternative if this is not possible.

  2. #2
    Join Date
    Apr 2004
    Posts
    102

    Re: Importing Variables From Outside File

    One option would be to write and read a structure containing all the required info to/from a ASCII text file.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct rec
    {
        int  var1;
        char var2[256];
    };
    
    int main(void)
    {
        FILE *f;
        struct rec r, t;
        f=fopen("myFile.txt","w");
        if (!f)
            return 1;
        r.var1 = 15;
        strcpy(r.var2, "C:/Files/run.exe" );
        fwrite(&r,sizeof(struct rec),1,f);
        fclose(f);
    
        f=fopen("myFile.txt","r");
        if (!f)
            return 1;
        fread(&t,sizeof(struct rec),1,f);
        printf("var1 = %d\nvar2 = %s\n",r.var1,r.var2);
        fclose(f);
        return 0;
    }

  3. #3
    Join Date
    Apr 2008
    Posts
    725

    Re: Importing Variables From Outside File

    urgh. why use out-dated headers when there are perfectly good c++ standard equivalents??

    please see <fstream>, <string> and <iostream>

  4. #4
    Join Date
    Apr 2004
    Posts
    102

    Re: Importing Variables From Outside File

    Quote Originally Posted by Amleto View Post
    urgh. why use out-dated headers when there are perfectly good c++ standard equivalents??

    please see <fstream>, <string> and <iostream>
    Hmmm... Maybe somebody should notify the International Organization of Standards that their C99 C standard is out-dated.

  5. #5
    Join Date
    Aug 2007
    Posts
    858

    Re: Importing Variables From Outside File

    I'm looking to be able to import variables from an outside source. If i were to create a file that said:
    var1 = 15
    var2=C:/Files/run.exe
    How would i accomplish this? What would be the best alternative if this is not possible.
    1. Open the file in question.
    2. Read a line from the file as a string
    3. Parse that string into an identifier ("var1") and a value ("15")
    4. Store the identifier and value using some method (std::map will do nicely)
    5. If you're not at the end of the file, go to 2
    6. Do something useful with the information you read
    7. ...
    8. Profit!

    Maybe somebody should notify the International Organization of Standards that their C99 C standard is out-dated.
    If you're using C++ they're out of date unless you have a specific need for them (usually to support other legacy code).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured