Thread: How to read a Value from Text File and store it in Variables

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    4

    How to read a Value from Text File and store it in Variables

    Hi guys, this is my text file (Data.txt):

    rho_0 10
    kp_0 8
    Beta_kp 6
    x_min 5
    x_max 8
    y_min 9
    y_max 5
    z_min 4
    z_max 7

    I want to read from this text file line by line and store each Value in the parameter at the same line. for example, for the first line store 10 in rho_0. I have written as below.At the next end I want to use x_min and x_max ... to calculate Volume. I mean
    Volume=(x_max-x_min)*(y_max-y_min)*(z_max-z_min)
    I have written the code as below and it works with Code::Blocks but When I want to run it in Microsoft Visual Studio, it gives me these error as below :

    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(63): error C2143: syntax error : missing ';' before 'type'
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(65): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(66): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(67): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(68): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(69): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(70): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(71): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(72): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(73): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(74): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(75): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(76): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(77): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(78): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(79): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(80): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(81): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(82): error C2065: 'storage' : undeclared identifier
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(84): error C2143: syntax error : missing ';' before 'type'
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(86): error C2065: 's' : undeclared identifier
    1>
    1>Build FAILED.



    Can some one please tell me, what is the problem?
    This is also my code:

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    int main()
    {
        double rho_0, kp_0, Beta_kp;
        double x_min, x_max ,y_min ,y_max ,z_min, z_max, V_plasmazone;
        FILE *inputfile;
    
    
        /*char inputpath[100];
        char path [100] ;
        strcpy(inputpath, path);
        strcat(inputpath, "Data.txt");
        inputfile = fopen(inputpath, "r");
    
    
        if(inputfile)
        {
            ScanCheck(fscanf(inputfile, "%s", inputpath), 1, inputfile, "Data.txt", 1);
            strcat(path, inputpath);
            strcat(path, "/");
        }
        else
        {
            Error("Error opening input.txt - file!");
        }*/
    
    
      inputfile =fopen ("Data.txt","r") ;
        if(inputfile == NULL) {
                   perror("Unable to open file!");
                   exit (1);
                }
    
    
       char storage[100];
    
    
       fscanf(inputfile, "%s %lf\n", storage, &rho_0);
       printf("%s %lf\n", storage, rho_0);
       fscanf(inputfile, "%s %lf\n", storage, &kp_0);
       printf("%s %lf\n", storage, kp_0);
       fscanf(inputfile, "%s %lf\n", storage, &Beta_kp);
       printf("%s %lf\n", storage, Beta_kp);
       fscanf(inputfile, "%s %lf\n", storage, &x_min);
       printf("%s %lf\n", storage, x_min);
       fscanf(inputfile, "%s %lf\n", storage, &x_max);
       printf("%s %lf\n", storage, x_max) ;
       fscanf(inputfile, "%s %lf\n", storage, &y_min);
       printf("%s %lf\n", storage, y_min);
       fscanf(inputfile, "%s %lf\n", storage, &y_max);
       printf("%s %lf\n", storage, y_max);
       fscanf(inputfile, "%s %lf\n", storage, &z_min);
       printf("%s %lf\n", storage, z_min);
       fscanf(inputfile, "%s %lf\n", storage, &z_max);
       printf("%s %lf\n", storage, z_max);
    
    
     char s[100] = "V_plasmazone";
     V_plasmazone = ( x_max - x_min )*( y_max - y_min )*( z_max - z_min );
     printf( "%s %lf\n", s, V_plasmazone );
    
    
                  fclose(inputfile);
                  return 0;
    
    }

    Last edited by Cfdman; 4 Weeks Ago at 02:19 AM. Reason: wrong typed

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,076
    Are you sure you managed to compile it with your compiler under Code Blocks? The code has compile errors, e.g., you use the variable named V_plasmazone without declaring it. Furthermore, MSVC makes reference to line numbers that don't exist in the code that you posted.

    EDIT: oh, I'm mistaken, you did declare it, but right at the top.
    Last edited by laserlight; 4 Weeks Ago at 02:40 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    4
    Thanks for reply, actually I'm not a very professional in C.
    I have written the code in Clode Block under C and during compiling and running I did not recieve any error.
    And what do you mean by using the variable named V_plasmazone without declaring it ? because as you see I have declarated all the Vaiable at the first (double V_plasmazone ). Could you please explain it more?

    EDIT: Just now I have seen, that you understood your mistake

    The version, which I'm using, does not
    make reference to line number.
    MSV 2010
    Last edited by Cfdman; 4 Weeks Ago at 02:56 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,076
    Okay, to avoid any possible misreading of your code, I compiled this with gcc 9.3.0 with the -Wall -pedantic -std=c99 flags:
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    int main()
    {
        double rho_0, kp_0, Beta_kp;
        double x_min, x_max ,y_min ,y_max ,z_min, z_max, V_plasmazone;
        FILE *inputfile;
    
        inputfile = fopen("Data.txt","r");
        if (inputfile == NULL) {
            perror("Unable to open file!");
            exit(1);
        }
    
        char storage[100];
    
        fscanf(inputfile, "%s %lf\n", storage, &rho_0);
        printf("%s %lf\n", storage, rho_0);
        fscanf(inputfile, "%s %lf\n", storage, &kp_0);
        printf("%s %lf\n", storage, kp_0);
        fscanf(inputfile, "%s %lf\n", storage, &Beta_kp);
        printf("%s %lf\n", storage, Beta_kp);
        fscanf(inputfile, "%s %lf\n", storage, &x_min);
        printf("%s %lf\n", storage, x_min);
        fscanf(inputfile, "%s %lf\n", storage, &x_max);
        printf("%s %lf\n", storage, x_max) ;
        fscanf(inputfile, "%s %lf\n", storage, &y_min);
        printf("%s %lf\n", storage, y_min);
        fscanf(inputfile, "%s %lf\n", storage, &y_max);
        printf("%s %lf\n", storage, y_max);
        fscanf(inputfile, "%s %lf\n", storage, &z_min);
        printf("%s %lf\n", storage, z_min);
        fscanf(inputfile, "%s %lf\n", storage, &z_max);
        printf("%s %lf\n", storage, z_max);
    
        char s[100] = "V_plasmazone";
        V_plasmazone = ( x_max - x_min )*( y_max - y_min )*( z_max - z_min );
        printf( "%s %lf\n", s, V_plasmazone );
    
        fclose(inputfile);
        return 0;
    }
    gcc did not report any diagnostic, not even a warning. I suggest that you try compiling the exact same code with MSVC. If that fails, I suggest two things:
    • Move the declaration of storage and s to the top of the block, along with the other declarations. (Actually, s is quite pointless: you should just use "V_plasmazone %lf\n" as the format string instead of having s.)
    • Change %lf to %f. %lf for printf is equivalent to %f, but it was introduced in C99 for consistency with %lf for scanf, where it does make a difference. However, MSVC historically was slow to adopt features introduced in C99 or later, so that could be an issue.


    EDIT:
    Quote Originally Posted by Cfdman
    The version, which I'm using, does not make reference to line number.
    MSV 2010
    It does. Look at this:
    Code:
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(63): error C2143: syntax error : missing ';' before 'type'
    The (63) refers to line number 63 in the specified file.
    Last edited by laserlight; 4 Weeks Ago at 03:10 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2020
    Posts
    4
    Thanks alot fo your help. Just 2 question :
    1-I just moved the decleration of s and strorage to the top, and now it's working but can you roughly explain me why in MSV, you must declerate them at the top of the block, along with the other declarations? but in code::block not?

    2-and also in Outpu window I get some Warning like :
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(36): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(234) : see declaration of 'fopen'
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(46): warning C4996: 'fscanf': This function or variable may be unsafe. Consider using fscanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(253) : see declaration of 'fscanf'

    Are they important? or I should ignore them? because the code runs and I can get the result.
    Acually I'm going to use this code in UDF(User Define Function) of Fluent Software, UDF use actually C Librariry.
    Can these warning cause some problems there?

    again thanks alot for your nice explanations.

  6. #6
    Registered User
    Join Date
    Sep 2020
    Posts
    38
    1.) In older versions of C you had to declare the vars at the top of a function. Your VS 2010 doesn't support modern C.
    Code::Blocks uses GCC which is more modern.

    2.) These is just the Microsoft opion that these function are deprecated - you can safely ignore them. The suggested fopen_s is not part of the standard.

    In general using VS for C programming isn't the best choice. Code::Blocks or Pelles IDE are better choice, IMHO.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,076
    Quote Originally Posted by Cfdman
    1-I just moved the decleration of s and strorage to the top, and now it's working but can you roughly explain me why in MSV, you must declerate them at the top of the block, along with the other declarations? but in code::block not?
    During the Cretaceous period, variables had to be declared at the start of blocks. It was forbidden to mix declarations and non-declarative code. MSVC evolved very slowly, so it is quite possible that Visual Studio 2010 still kept to this prehistoric requirement of C.

    Note that Code::Blocks is an IDE, not a compiler.

    Incidentally, a quick check shows that support for Visual Studio 2010 ended on July 14, 2020. It is time to upgrade. In fact, this article notes that support for C11 and C17 is available as of Visual Studio 2019.

    Oh, and again, you don't need s: just use an appropriate format string when printing, e.g.,
    Code:
    printf( "V_plasmazone %lf\n", V_plasmazone );
    If you insist on having s, then you must rename it: s is not a descriptive name.

    Quote Originally Posted by Cfdman
    2-and also in Outpu window I get some Warning like :
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(36): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(234) : see declaration of 'fopen'
    1>c:\users\documents\visual studio 2010\projects\funktion\funktion\main.c(46): warning C4996: 'fscanf': This function or variable may be unsafe. Consider using fscanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(253) : see declaration of 'fscanf'

    Are they important? or I should ignore them? because the code runs and I can get the result.
    Acually I'm going to use this code in UDF(User Define Function) of Fluent Software, UDF use actually C Librariry.
    These warning can cause some problems there?
    The warnings told you to "see online help for details", so you should have done that first.

    Anyway, I'm of the opinion that while well-intentioned, Microsoft's "security features" suck. They "don't prevent or correct security errors. Instead, they catch errors when they occur" while making your code less portable. So what I would do is define _CRT_SECURE_NO_WARNINGS, and then prevent and correct security errors by a combination of using the functions carefully and/or defining wrapper functions that encourage correct use, along with plenty of (automated) testing and peer review.

    Having said that, it is true that your code is vulnerable to buffer overflow. For example:
    Code:
    fscanf(inputfile, "%s %lf\n", storage, &rho_0);
    storage is an array of 100 chars, with one char reserved for the terminating null character. If instead of "rho_0" the file contains a "word" of more than 99 characters, you'll have buffer overflow. What you should have done is something like this:
    Code:
    if (fscanf(inputfile, "%99s %lf\n", storage, &rho_0) != 2 || strcmp(storage, "rho_0") != 0) {
        // invalid input
        // ...
    }
    I might rename storage to something like param_name to make it clear that it stores the current expected parameter name.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2020
    Posts
    4
    Guys thanks you so much for your help
    It was very useful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-07-2020, 06:42 PM
  2. Read Strings in from text file and store into array
    By RRTT in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2011, 04:52 AM
  3. Read text file, scan and store data into variables?
    By wisdom30 in forum C Programming
    Replies: 8
    Last Post: 04-18-2011, 11:23 PM
  4. Read text file and store it in an array
    By look2hook in forum C Programming
    Replies: 2
    Last Post: 12-03-2010, 11:47 PM
  5. read and store text file as an array
    By abotaha in forum C++ Programming
    Replies: 1
    Last Post: 08-02-2010, 08:57 PM

Tags for this Thread