Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Reading / searching /writing including IP addresses.

Status
Not open for further replies.

Rhoon

Programmer
Mar 23, 2001
55
US
Hey all,

I've been kind of snooping this forum for a few days and I think its one of the best sources for information out on the web today, BTW: I'm loving all the links to C programming everyone is putting out, great reads :>

Anyhow, I'm sort of new to C programming and I have a question about Strings and Chars...

From what I've read for the past few months...

char *string /*will create a pointer to a char of
indeterminate size */

I'm trying to get a hang of file inputs and outputs.

1) Whats the best function to use to read from a file
that is 20 megs in size (A text file thank god).
2) Whats the best function to write to a file.
3) How does one create a new file that doesn't already
exist.
4) When prompting the user for the name of the file to be worked on, is it better to store the name of the file as a FILE * , or as a char[Max_size] w/ max_size = 100;

5) I'm also picking out of the files IP address's and host names, I'm curious in whats the best way to read these?
IE: 255.255.255.255 format out formats like whitehouse.gov


Any and all help is much appreciated and my thanks in advance.

Andrew

perihelion_mg@hotmail.com
 
>Hey all,

Hi

>I've been kind of snooping this forum for a few days and I think its one of the best sources for information out on the web today, BTW: I'm loving all the links to C programming everyone is putting out, great reads :>

>Anyhow, I'm sort of new to C programming and I have a question about Strings and Chars...

>From what I've read for the past few months...

>char *string /*will create a pointer to a char of
> indeterminate size */

>I'm trying to get a hang of file inputs and outputs.

>1) Whats the best function to use to read from a file
> that is 20 megs in size (A text file thank god).

If it's a text file, you can use fgets() to read the file line by line. Of course it depends on how you need to parse the content, but fgets() is a good general purpose function for reading text files.

>2) Whats the best function to write to a file.

If it's a text file, fprintf() is good because it allows for sophisticated formatting of output.

>3) How does one create a new file that doesn't already
> exist.

With the standard function fopen():

FILE *fp=fopen("new_file","w");

This either creates a new file called "new_file" or if a file already exists with this name, it overwrites it.

>4) When prompting the user for the name of the file to be worked on, is it better to store the name of the file as a FILE * , or as a char[Max_size] w/ max_size = 100;

A FILE * object isn't intended for this purpose, so you'll want to use an array of char. FILENAME_MAX, defined in <stdio.h>, is the maximum number of characters allowed for a file name by the implementation:

char fname[FILENAME_MAX+1];

>5) I'm also picking out of the files IP address's and host names, I'm curious in whats the best way to read these?
>IE: 255.255.255.255 format out formats like whitehouse.gov

Probably using a combination of fgets() and sscanf() or fgets() and strtok(). fscanf() might be a possibility too, but unless you're absolutely sure about the integrity and format of the file, this is probably unwise because it becomes unwieldy when things aren't the way you expect them to be.

>Any and all help is much appreciated and my thanks in advance.

HTH,

Russ
bobbitts@hotmail.com
 
One other thing on your post:

char *string /*will create a pointer to a char of
indeterminate size */

This is not exactly right, this creates a pointer to char which is a determinate size -- sizeof(char *). However, it points to an indeterminate /address/ when declared if it's an auto variable, otherwise it is initialized to NULL and points nowhere.

In either case, you shouldn't dereference the pointer until you set it to point to memory that you own, either through dynamic allocation + initialization:

char *string=malloc(100);
strcpy(string,&quot;a string&quot;);

or by setting it to point to some already initialized memory:

char buf[]=&quot;hello&quot;;
char *string=buf;

or by initializing it to point to a string constant:

char *string=&quot;hello&quot;;

In the last case, it is not safe to modify the memory pointed to by string as &quot;hello&quot; could very well have been stored in read-only memory.

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top