LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-30-2012, 10:47 AM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092
Blog Entries: 1

Rep: Reputation: 90
C: what's the right structure to hold a (previously unkown sized) file in memory?


Hi!

I have to read an mcrypt-encrypted file from FS and I want to keep it in memory.

The thing is that I don't know before-hand the size of the file so I don't know the size of the char * I will have to use to hold its content.

So, the structure I'm using to hold the file content is called file_content. Given that I will have to dynamically allocate the char* content I will use a char**, right?

So:

Code:
char ** file_content;
I allocate it with this:

Code:
file_content = malloc(sizeof(char) * st.st_size);
st.st_size being the size of the file from stat.h. Now, when I start reading the file I should start setting each of the chars in *file_content, right?

So:
Code:
i = 0;
char aChar;
while (i < st.st_size && fread(&aChar, 1, 1, mcrypt_input) == 1 ) {
    mdecrypt_generic (td, &aChar, 1);
    *file_content[i] = aChar;
    i++;
}
Ain't that right? Well, I know it ain't right cause I'm getting a seg-fault. So what am I missing?

Thanks in advance.
 
Old 07-30-2012, 11:03 AM   #2
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092

Original Poster
Blog Entries: 1

Rep: Reputation: 90
By the way, segfault is here:

Code:
*file_content[i] = aChar;
 
Old 07-30-2012, 11:18 AM   #3
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092

Original Poster
Blog Entries: 1

Rep: Reputation: 90
Well.... I changed file_content to char* and now it works (with the corresponding changes in the rest of the code).
 
  


Reply

Tags
char *, char **, gcc, malloc, segfault



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unkown file! Help please! voyciz Linux - Newbie 13 08-13-2010 08:19 AM
Convert directory structure from long file names in Linux to DOS 8.3 structure? manorina Linux - Software 5 09-12-2009 09:18 AM
Unkown proccess in CPU/Memory/MySQL Usage of WHM centosfan Linux - Security 4 02-19-2009 07:43 PM
File recovery from unkown embedded OS FS sprzybysz Linux - Software 1 08-17-2006 04:30 PM
Dynamically sized objects in shared memory jtshaw Programming 2 03-16-2005 10:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration