Quantcast

Jump to content


Photo

File IO in C


  • Please log in to reply
4 replies to this topic

#1 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 16 February 2011 - 02:22 PM

I have finally run into what I hate the most about C, dynamic arrays. Or rather, the lack thereof.

All I want is to store the content of a text file in a char array. In any other language this is easily achievable. With Ruby, in 1 line of code, if I'm not mistaken.

Anyways, the content and size of the file is unknown. So far I have this:

#include <string.h>
#include <stdio.h>
#include <pcre.h>

int main()
{
	/* open json-sample-object and start reading it */
	FILE *fin;
	
	fin = fopen("/home/negrabee/playin-with-c/json-sample-object", "r");
	
	if(fin == NULL)
	{
		printf("Error opening file\n");
		return 1;
	}	
	
	char buffer[128];
	
	while(!feof(fin))
	{	
         fgets(buffer, 128, fin);


        //this is where I blank out 

	}
}

So at that point in the loop, I'm unsure what to do. I mean I would like to concatenate a char array with 'buffer' but to that I would need to resize the array each time. Which is surprisingly a lot to ask for.


Any ideas guys?



#2 Hydrogen

Hydrogen
  • Neocodex Co-Founder

  • 22213 posts


Users Awards

Posted 16 February 2011 - 03:12 PM

You don't have to resize the array each time if you allocate a large enough character array to store the entire file. If you're parsing json, perhaps your json library has a parse file utility function that will let you avoid doing this. In general, C has you allocate a character array much larger than you need and terminates the cstring by a null character so it knows where to stop. If the file is too big to fit in memory, perhaps you should alter your parsing algorithm to not expect the entire file at once, but allow for parts of it to come in at a time. That way, you can read a set amount of characters at a time, avoid allocating a huge amount of memory, and just move the file pointer along as you read.

Also, you should call fclose(fin) once you're done with the file descriptor.

#3 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 16 February 2011 - 03:48 PM

You don't have to resize the array each time if you allocate a large enough character array to store the entire file. If you're parsing json, perhaps your json library has a parse file utility function that will let you avoid doing this. In general, C has you allocate a character array much larger than you need and terminates the cstring by a null character so it knows where to stop. If the file is too big to fit in memory, perhaps you should alter your parsing algorithm to not expect the entire file at once, but allow for parts of it to come in at a time. That way, you can read a set amount of characters at a time, avoid allocating a huge amount of memory, and just move the file pointer along as you read.

Also, you should call fclose(fin) once you're done with the file descriptor.

You are very clever Hydro :p


Literally 20 mins ago, my partner and I found out that release 1.5 of wireshark has a JSON library which basically makes all of this obsolete. >_< 2 days of coding wasted =P

Anyways, I'd still like to learn more about C arrays. I want to avoid fixed arrays, since the json objects could be very small(50 chars) or very large(5000 chars).

I'll keep working on this...

Thanks for your input :)



#4 Hydrogen

Hydrogen
  • Neocodex Co-Founder

  • 22213 posts


Users Awards

Posted 16 February 2011 - 05:50 PM

You are very clever Hydro :p

I realize this :p.

Good luck. If you need anything else, I'd be happy to help out.

What protocol are you writing a disector for, by the way?

#5 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 16 February 2011 - 08:30 PM

I realize this :p.

Good luck. If you need anything else, I'd be happy to help out.

What protocol are you writing a disector for, by the way?


You asked me this already =P

It's called CDMI(Cloud Data Management Interface). Created by someone working in our company. Cloud storage is supposedly being used more frequently and the dev and qa teams need a tool for monitoring network traffic on cdmi servers.


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users