Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

fread not reading all the characters all the time...

i am trying to read in a config.txt file to set the config options for my program.  the config file uses return characters to delimit the different settings.  the first line to read is simply an ip address with a colon and a port number...half of the time when i call fread it works fine...half of the time it skips the first 4 digits (ie. 67.90.xx.xx:50xx  vs. 0.xx.xx:50xx).  code and config file are listed below...

if i run this 10 times in a row about 50/50 on how it reads in the config.txt file
my code***

char *configfilename = "C:\\Users\\Administrator\\Desktop\\config.txt";
	char configbuffer[100]= "";
	char *domain;
	char domainuri[20] = "sip:";
	char *user;
	char *pass;
	char *copath;
	char sipstring[80] = "sip:";	
				
	configfile = fopen(configfilename, "r+");
	fread(configbuffer, 1, 98, configfile);
	
	//configbuffer[99] = 0;
	domain = strtok(configbuffer, "\n");
	user = strtok(NULL, "\n");
	pass = strtok(NULL, "\n");
	copath = strtok(NULL, "\n");
        fclose(configfile);


my config file***

67.90.xx.xx:50xx
N38N87NXX16N3N1
BJD892JXXDK93JSL9DJ4
C:\\co\\*.co

Open in new window

0
csummer
Asked:
csummer
  • 7
  • 5
  • 5
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi csummer,

That's an odd way to read a config file, but it should work just fine.

A much more versatile way is to read one line at a time.  That makes managing the file much easier if the file ever changes.


But as to the code that you've posted, I see nothing that should cause the randomness that you describe.


Kent
0
 
Infinity08Commented:
>> but it should work just fine.

Not necessarily. If the total size of the file (including newlines) is longer than 98 bytes, you won't read enough. Since you don't null-terminate the read data, strtok might not work correctly either.

Granted, this should not give the behavior the author describes at the beginning of the file, assuming that the code that was posted is representative.

But they're problems that should be addressed nevertheless (preferably by reading the data as a text file, line by line as Kdo already suggested).
0
 
csummerAuthor Commented:
me either...but i assure you its happening...

we are mostly a .net/java shop and i showed our other programmers and they are just as puzzled as i am regarding this...i hoped i was just doing something stupid or silly...

the reason why i didnt do read line was because fread reads in the whole file in one operation where as fread only does one line at a time...sure its not gonna be a large difference in this situation but its old coding habit...

on a side note which may or may not be related...when i am reading and working with strings...sometimes in my debugger some of my strings appear as asian characters...even though it will resolve to the proper english characters when called.  i was wondering if i may have the wrong character set or so chosen for this project...but i have no idea how that got messed up...i rarely edit the detailed config settings in visual studio...

anybody else have any ideas??
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kent OlsenData Warehouse Architect / DBACommented:
Hi csummer,

Another thought.  :)  (They come randomly some times....)

Based on the path name in your code, you're running on a Windows system.  If your configuration file is created as a Windows or DOS file, lines are terminated by a carriage return AND line feed.  (If it's a unix formatted file, only the line feed character is used.)

You're calling strtok() to separate the tokens.  But scanning only for '\n', each string will likely be terminated by a '\r' character.  That may cause an issue when you go to display the value!

Replace the string "\n" with "\r\n" in the call to strtok.  That will cause strtok to split the tokens at either a carriage return or line feed, and skip consecutive CR or LF characters so that neither will appear in your string.


Good Luck,
Kent
0
 
Infinity08Commented:
>> the reason why i didnt do read line was because fread reads in the whole file in one operation where as fread only does one line at a time...sure its not gonna be a large difference in this situation but its old coding habit...

If you're doing this for reason of performance - don't bother. Let the underlying system deal with file buffering ;) It does a pretty good job most if the time.

>> but i assure you its happening...

Could you show the exact and complete code ? Or if you can't provide that, at least a fully compilable sample code that exhibits the same problem ?
0
 
Infinity08Commented:
@Kdo - that wouldn't give the issue on the first line of the file though;
0
 
Kent OlsenData Warehouse Architect / DBACommented:
>> the reason why i didnt do read line was because fread reads in the whole file in one operation where as fread only does one line at a time...sure its not gonna be a large difference in this situation but its old coding habit...

C will block and buffer the input so that each call to fgets() moves the line from the buffer until the buffer is exhausted and another physical read is necessary.  fgets() does not automatically read from disk for every call.


0
 
Infinity08Commented:
>> sometimes in my debugger some of my strings appear as asian characters...

Are you reading the 4 strings read from the file with your debugger ?

If so, try printing them out to standard output instead to see if they contain the correct data. Place this after the fclose call :
fprintf(stdout, "domain : %s\n", domain);
fprintf(stdout, "user : %s\n", user);
fprintf(stdout, "pass : %s\n", pass);
fprintf(stdout, "copath : %s\n", copath);

Open in new window

0
 
Kent OlsenData Warehouse Architect / DBACommented:
>> that wouldn't give the issue on the first line of the file though;

Maybe, maybe not.  I'd like to see the strings printed as hex instead of ASCII.  It would be interesting to see exactly what the strings really contain.

If the strings contain what csummer is actually seeing, it suggests that the error doesn't really lie with this code, but that the program is already corrupt and this is just a symptom....

Kent
0
 
csummerAuthor Commented:
the strtok was working fine before...its funny cause when it compiles and works...i am then able to run the exe and it always seems to work...but every other time it misses the first 4 characters when reading in the config.txt file...

this is at the top of my code ...perhaps im using the wrong libraries???
#include <stdlib.h>
#include <windows.h>

im not entirely sure what other code you would need to see...realistically as soon as it opens the file and freads its already wrong...the code compiles and works (if incorrectly)...

im gonna try to use the fgets instead to see how that does...but this is really weird behavior...

thanks...
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Do you always read the same configuration file?

0
 
Infinity08Commented:
>> im not entirely sure what other code you would need to see...

If what you posted is exactly what you use, with nothing left out, nothing modified, and nothing added, then that's ok.

Do try to place the fprintf lines I mentioned earlier after the fclose call to see if it's a problem with your debugger not being able to read the variables correctly.
0
 
csummerAuthor Commented:
yes i always use the same config.txt file...

so i changed the code in order to output the config buffer as shown in the code below...right after the fread i use "puts(configbuffer);" and it printed out the whole file correctly...but when i copy and paste from the locals window (while debugging) configbuffer looks like this

"-            configbuffer      0x001cfbd4 "0.16.6:5070
E386873161301
9OG7VWVO4PWPMEH3FOUR
C:\\co\\*.co"      char [100]
"

configbuffer[0] = 48'0'
configbuffer[1] = 46'.'
configbuffer[2] = 49'1'
etc...

why would puts(configbuffer) print out to the console correctly when the buffer file seems to be missing the first 4 characters...

it may be possible to get around this behavior...i just wish i new why it was behaving this way...
0
 
csummerAuthor Commented:
i forgot to add how i changed the code... here it is
FILE *configfile;
	//char *configfilename = "c:\\Program Files\\PCDispatch\\callout3\\config.txt";
	char *configfilename = "C:\\Users\\Administrator\\Desktop\\config.txt";
	char configbuffer[100]= "";
	char *domain;
	char domainuri[20] = "sip:";
	char *user;
	char *pass;
	char *copath;
	char sipstring[80] = "sip:";
	
				
	configfile = fopen(configfilename, "r+");
	fread(configbuffer, 1, 95, configfile);
	puts(configbuffer);
	
	//configbuffer[99] = 0;
	domain = strtok(configbuffer, "\n");
	user = strtok(NULL, "\n");
	pass = strtok(NULL, "\n");
	copath = strtok(NULL, "\n");

	fclose(configfile);

Open in new window

0
 
Infinity08Commented:
Debuggers aren't always able to retrieve the information correctly. Especially not if the project settings haven't been set correctly. You are compiling the debug version, aren't you ? And you don't strip any debugging symbols out ?

Either way, if the puts or fprintf statements print the right values (did you try adding the 4 I mentioned earlier ?), then the code is working just fine. It's just your debugger that has an issue ...
0
 
csummerAuthor Commented:
ok....i have been struggling to get this program to compile correctly with the libraries i am using...i just realized that i compiled a release version to test out and i never changed my settings back from release to debug...when i found a bug i just started debugging in release mode and from what i understand now, because the debugging mode wasnt set correctly my debugger was showing the wrong info...

i changed it back to debug mode and so far 10/10 it has shown the debugging info correctly...this means that the error i thought i had lies somewhere else and now that i know that the debugging tools can be incorrect...im planning on outputting any data im curious about to the console...

thanks for the help...
0
 
Infinity08Commented:
No worries :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 7
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now