File read help

This is a discussion on File read help within the C Programming forums, part of the General Programming Boards category; Hello, I am trying to use a switch statement to read data from a text file. Text File is laid ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    24

    File read help

    Hello,

    I am trying to use a switch statement to read data from a text file. Text File is laid out as follows:

    Code:
    val1=1
    val2=2
    ...
    string1=help
    string2=please
    My code (as shown below) will not compile because it says that my case expression has to be a constant. I thought I did make it a constant. Any suggestions?

    Also, is this the best method to read data from a text file and assign it to a variable within a program?

    Thanks!

    Code:
    #include	<stdlib.h>
    #include	<stdio.h>
    #include	<string.h>
    
    #define	IniTestFile	"test.ini"
    #define TestLogFile "test.log"
    
    static FILE *ini_file;
    static FILE *log_file;
    
    int main(void)
    {
    	int total = 0, value1 = 0, value2 = 0;
    	
    	if ((log_file = fopen(TestLogFile, "a+") ) == NULL)
    		fprintf (stderr, "\nERROR - Cannot open <%s>\n", TestLogFile);
    	else{
    		fprintf(log_file, "\nProgram starting");
    	}
    
    	if ((ini_file = fopen(IniTestFile, "r") ) == NULL)
    		fprintf (stderr, "\nERROR - Cannot open <%s>\n", IniTestFile);
    	else{
    		fprintf(log_file, "\nIni File Open");
    	}
    	
    	const char var1[4], val1[4], val2[4];
    	int var2;
    
    	char Variable = fscanf(ini_file, "%[^=] %s", var1, &var2 );
    
    	while ( Variable =! EOF ) {
    		Variable = fscanf( ini_file, "%[^=] %s", var1, &var2 );
    		switch ( var1 ) {
    			case "val1":
    				value1 = var2;
    				break;
    			case "val2":
    				value2 = var2;
    				break;
    		}
    	}
    
    	total = value1 + value2;
    	fprintf(log_file, "\nTotal is: %d", total);
    
    	fclose(log_file);
    	fclose(ini_file);
    
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, "Variable", other than being a horrible variable name, cannot possibly store the EOF marker. When testing for EOF, you should use an int instead. Also, I would probably use something like fgets combine with sscanf to read an entire line, then scan the portions of it I want.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    24
    Thanks Quzah. I changed "Variable" into "LineRead" and from a char to an int.

    As for the rest of your suggestions, do you think that the fscanf statement is the problem with my case expression? The error I'm getting is that my case expression has to be constant. I'm not sure what that has to do with fscanf...

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > case "val1":
    You can't do cases on strings

    It should be things like
    case 2:

    Oh, and you're in C99 mode with your C code.


    > Variable =! EOF
    Whilst syntactically legal, it's not what you want
    Try != instead
    Also, your variable should be an int, not a char

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    24
    OK...I'm trying something different now. The switch statement was not working for me. Looking through this forum, I found the GetPrivateProfileInt function and decided to try it instead. Here's what I've got so far:

    Code:
    #define	IniTestFile	"test.ini"
    #define TestLogFile "test.log"
    
    static FILE *ini_file;
    static FILE *log_file;
    
    int main(void)
    {
    	int total = 0;
    	
    	if ((log_file = fopen(TestLogFile, "a+") ) == NULL)
    		fprintf (stderr, "\nERROR - Cannot open <%s>\n", TestLogFile);
    	else{
    		fprintf(log_file, "\nProgram starting");
    	}
    
    	if ((ini_file = fopen(IniTestFile, "r") ) == NULL)
    		fprintf (stderr, "\nERROR - Cannot open <%s>\n", IniTestFile);
    	else{
    		fprintf(log_file, "\nIni File Open");
    	}
    	
    	char val1[4], val2[4], General[9];
    
    	int value1 = GetPrivateProfileInt("General","val1",3,IniTestFile);
    	int value2 = GetPrivateProfileInt(General,val2,4,IniTestFile);
    
    	total = value1 + value2;
    	fprintf(log_file, "\nTotal is: %d", total);
    
    	fclose(log_file);
    	fclose(ini_file);
    
    	return 0;
    }
    From my code, you can see that I tried two different approaches with "value1" and "value2". My compiler is telling me that GetPrivateProfileInt is an undeclared identifier. Since this is the first time I have tried using this member function, can anyone tell me what's wrong?

    Thanks!

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >GetPrivateProfileInt is an undeclared identifier.
    That's a windows function, so you should be including <windows.h>. Looking at your code, you should also be including <stdio.h>.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    24

    Question

    Thanks, that did the trick!

    Now that it's compiling, it does not seem to find the key name in the ini file (val1 or val2). GetPrivateProfileInt is just returning the default values that I put in (3 and 4). Any idea why that's happening? I tried changing the string lengths and names, but it made no difference. I'm not sure what else I can try.

    Thanks again!!

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Does you .ini file have a General section (a line with [General])?

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    24
    Yes. It looks like this:

    Code:
    [General]
    val1=1
    val2=1

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You may need to close your ini file before calling that function:
    Code:
    	if ((ini_file = fopen(IniTestFile, "r") ) == NULL)
    		fprintf (stderr, "\nERROR - Cannot open <%s>\n", IniTestFile);
    	else{
    		fprintf(log_file, "\nIni File Open");
    	}
    	fclose(ini_file);
    	
    	char val1[4], val2[4], General[9];
    
    	int value1 = GetPrivateProfileInt("General","val1",3,IniTestFile);
    	int value2 = GetPrivateProfileInt("General","val2",4,IniTestFile);

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    24
    Hey swoopy,

    I tried your suggestion and unfortunately, it still does not work. Any other ideas?

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I tried your suggestion and unfortunately, it still does not work. Any other ideas?
    Hmm, I ran some code like this, and it worked fine.
    Code:
       int value1,value2;
       if ((value1 = GetPrivateProfileInt("General","val1",0,filename)) == 0)
       {
          printf("value1 not found.\n");
          return EXIT_FAILURE;
       }
       printf("value1:%d\n",value1);
    
       if ((value2 = GetPrivateProfileInt("General","val2",0,filename)) == 0)
       {
          printf("value2 not found.\n");
          return EXIT_FAILURE;
       }
       printf("value2:%d\n",value2);
    
       return 0;
    Maybe it's not finding the file for some reason?

  13. #13
    Registered User
    Join Date
    Jun 2004
    Posts
    24
    Swoopy,

    Thanks again for all your help, but I seem to be up against a wall here. My program does not seem to recognize the "General" section and has an error reading val1 and val2. Can you compiling my code, along with my ini file and see if you get the same output? I have no compile errors or warnings and not sure what else I can do to get this thing working.

    Code:
    #include	<stdlib.h>
    #include	<stdio.h>
    #include	<string.h>
    #include	<windows.h>
    
    #define	IniTestFile	"test.ini"
    #define TestLogFile "test.log"
    
    static FILE *ini_file;
    static FILE *log_file;
    
    int main(void)
    {
    	int total = 0;
    	
    	if ((log_file = fopen(TestLogFile, "a+") ) == NULL)
    		fprintf (stderr, "\nERROR - Cannot open <%s>\n", TestLogFile);
    	else{
    		fprintf(log_file, "\nProgram starting");
    	}
    
    	if ((ini_file = fopen(IniTestFile, "r") ) == NULL)
    		fprintf (stderr, "\nERROR - Cannot open <%s>\n", IniTestFile);
    	else{
    		fprintf(log_file, "\nIni File Open");
    	}
    	
    	int value1;
    	int value2;
    	
    	if ( (value1 = GetPrivateProfileInt("General","val1",0,IniTestFile)) == 0 )
    		fprintf(log_file, "\nError reading val1");
    	if ( (value2 = GetPrivateProfileInt("General","val2",0,IniTestFile)) == 0 )
    		fprintf(log_file, "\nError reading val2");
    
    	total = value1 + value2;
    	fprintf(log_file, "\nTotal is: %d", total);
    
    	fclose(ini_file);
    	fclose(log_file);
    	
    	return 0;
    }
    INI file:
    Code:
    [General]
    val1=1
    val2=1
    Output:
    Code:
    Program starting
    Ini File Open
    Error reading val1
    Error reading val2
    Total is: 0

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Ok, I'm getting the same Output:
    Code:
    Program starting
    Ini File Open
    Error reading val1
    Error reading val2
    Total is: 0
    I'll take a look.

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    For the 4th parameter to GetPrivateProfileInt, use the full path.

    http://msdn.microsoft.com/library/de...profileint.asp:
    lpFileName
    [in] Pointer to a null-terminated string that specifies the name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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