Dynamic memory allocation in C using realloc - c

I have read the other SO question about using realloc to get a new pointer to the beginning of a bigger memory address space but I cant figure out what I am doing wrong. It prints a backtrace and memory dump. I later try to access strhldr but I dont think it even gets that far.
char *strhldr = (char *)malloc(strsize);
int chrctr = 0;
if(chrctr == strsize - 3){ // when you get close
strsize = strsize*2; //double size
char *temp = realloc(strhldr, strsize); //make more room
if(temp == NULL)
printf("reallocate failed\n");
else{
strhldr = temp;
free(temp); // removed same issue
}
}
// Later attempt to add it to an array
cmdargs[i] = strhldr;
This is all within a while loop where chrctr and strsize get incremented
complete code
int argctr = 64;
char **cmdargs = (char **) malloc(argctr * sizeof(char*));
char c = getchar();
int i = 0;
while(c != '\n' && c != EOF){ //read through a String of stdin
int strsize = 32;
char *strhldr = (char *)malloc(strsize);
char *strstarthldr = strhldr;
if(c == ' ')
c = getchar();
while(c != ' ' && c != '\n' && c != EOF){
int chrctr = 0;
if(chrctr == strsize - 3){ // when you get close
strsize = strsize*2; //double size
char *temp = realloc(strhldr, strsize); //make more room
if(temp == NULL)
printf("reallocate failed\n");
else
strhldr = temp;
} //add that word to the array of strings
strhldr[chrctr] = c;
chrctr++;
c = getchar();
}
strhldr[charctr] = '\0';
//strhldr = strstarthldr;
cmdargs[i] = strhldr;
i++;
}

On success, realloc will free its argument if needed. So remove the call to free(temp).

It's not very clear to me what you are trying to do but I believe free(temp); invalidates strhldr and future read/write access to it will cause you trouble.

Second problem - your value charctr (not chrctr) is not set. Here is a version of your loop. I haven't tested it but it should be close.
if(c == ' ') {
c = getchar();
}
int chrctr = 0;
while(c != ' ' && c != '\n' && c != EOF){
if(chrctr == strsize - 3){ // when you get close
strsize = strsize*2; //double size
char *temp = realloc(strhldr, strsize); //make more room
if(temp == NULL) {
printf("reallocate failed\n");
break;
}
else {
strhldr = temp;
}
}
strhldr[chrctr] = c;
chrctr++;
c = getchar();
}
strhldr[chrctr] = 0;

Related

malloc() in C returns populated memory

char *string = (char *) malloc(sizeof(char) * sz);
code right before this->void insert_word(word *root, char string1[], int linenumber) { int sz=strlen(string1)<=MAX_WORD_LENGTH?strlen(string1):MAX_WORD_LENGTH; Code block 3 has the entire context
Sometimes malloc() returns a populated memory location while using it.
What bothers me is that this is not random.
(This program consists of taking words from a file and passing them to this function. For THE SAME WORD, the function behaviour(in particular that of malloc()) is different.
For the inputs
string1=0x7fffffffdf10 "lol" root=BST, sz gets a value of 3
The value allocated to string by malloc() is 0x55555555c510 "\340\305UUUU" Why is malloc not pointing to an empty memory location? (This is not random behaviour, it is predictable and repeatable)
Furthermore,this loop runs an infinite amount of time for some reason
while(strcmp(string1,string)!=0)
{
free(string);
string=NULL;
string = (char *) malloc(sizeof(char) * sz);
strncpy(string,string1,sz);
}
MORE RELAVANT CODE
#define MAX_WORD_LENGTH 20
Definition of the structures
typedef struct linkedList
{
int number;
struct linkedList *next;
}list;
typedef struct word_with_count
{
char* string;
list *linenumbers;
struct word_with_count *left;
struct word_with_count *right;
}word;```
[3] ) The function
void insert_word(word *root, char string1[], int linenumber) {
int sz=strlen(string1)<=MAX_WORD_LENGTH?strlen(string1):MAX_WORD_LENGTH;
char *string = (char *) malloc(sizeof(char) * sz);
strncpy(string,string1,sz);
if (root==NULL) {
return;
} else if (strcmp(string, root->string) < 0) {
if (root->left == NULL) {
root->left = createword(string, linenumber);
} else {
insert_word(root->left, string, linenumber);
}
} else if (strcmp(string, root->string) > 0) {
if (root->right == NULL) {
root->right = createword(string, linenumber);
} else {
insert_word(root->right, string, linenumber);
}
} else {
append_list(linenumber, root->linenumbers);
}
free(string);
}
main() which calls this function
int main() {
char path[MAX_PATH_LENGTH];
FILE *fp;
fgets(path, MAX_PATH_LENGTH, stdin);
if (strlen(path) > 0 && path[strlen(path) - 1] == '\n')
path[strlen(path) - 1] = '\0';
fp = fopen(path, "r");
if (fp == NULL) {
printf("File not found\n");
return 0;
}
char ch;
int line_count = 1;
char current_word[MAX_WORD_LENGTH] = "";
word *root = NULL;
while (!feof(fp)) {
ch = fgetc(fp);
//printf("%c", ch);
if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') {
if (ch >= 'A' && ch <= 'Z')
ch = ch - 'A' + 'a';
strncat(current_word, &ch, 1);
} else if (ch == '-') {
continue;
} else {
if (strlen(current_word) > 2) {
if (root == NULL) {
root = createword(current_word, line_count);
} else {
insert_word(root, current_word, line_count);
}
}
memset(current_word, 0, sizeof(current_word));
if (ch == '\n') {
line_count++;
}
}
}
if (strlen(current_word) > 2) {
if (root == NULL) {
root = createword(current_word, line_count);
} else {
insert_word(root, current_word, line_count);
}
}
fclose(fp);
// print_tree(root);
//printf("\n");
//print_tree(root);
int status=delete_low_ocurrence(root, NULL, 3);
if (status == -1)root = NULL;
print_tree(root);
freetree(root);
return 0;
}
5)Auxilary function used by this function
word* createword(char string[], int linenumber)
{
word *newword = (word*)malloc(sizeof(word));
int sz=strlen(string)<=MAX_WORD_LENGTH?strlen(string):MAX_WORD_LENGTH;
newword->string = (char*)malloc(sizeof(char)*sz);
strncpy(newword->string, string,sz);
newword->linenumbers = (list*)malloc(sizeof(list));
newword->linenumbers->number = linenumber;
newword->linenumbers->next = NULL;
newword->left = NULL;
newword->right = NULL;
return newword;
}
Textfile given as input
much2f
much3f
lol
lol
lol
qwertyuiopasdfghjklzxcvbnmqwertyuiop
qwertyuiopasdfghjklzxcvbnmqwertyuiop
qwertyuiopasdfghjklzxcvbnmqwertyuiop
qwertyuiopasdfghjklzxcvbnmqwertyuiop
Why is malloc not pointing to an empty memory location?
Because it can. The content of the allocated memory via malloc() is not specified.
If code needs zeroed out memory, see calloc().
Bad code
strncpy(string,string1,sz) does not result in string being a string as it may lack null character termination. The following (strcmp(string... is then undefined behavior. Instead, do not use strncpy(), use strcpy() and make certain the prior allocation has enough room for the terminating null character.
strncpy(string,string1,sz);
...
} else if (strcmp(string, root->string) < 0) { // bad
Repaired code
word* createword(const char string[], int linenumber) {
word *newword = calloc(1, sizeof *newword);
size_t length = strlen(string);
if (length > MAX_WORD_LENGTH) {
length = MAX_WORD_LENGTH;
}
char *s = malloc(length + 1); // Include room for the \0
list *linenumbers = calloc(1, sizeof *linenumbers);
// Test allocation success
if (newword == NULL || s == NULL || linenumbers == NULL) {
free(newword);
free(s);
free(linenumbers);
return NULL;
}
memcpy(s, string, length); // Only copy the first 'length' characters.
s[length] = 0;
newword->string = s;
newword->linenumbers = linenumbers;
newword->linenumbers->number = linenumber;
newword->linenumbers->next = NULL;
newword->left = NULL;
newword->right = NULL;
return newword;
}
Why is “while ( !feof (file) )” always wrong?
feof(fp) improperly used here. fgetc() returns 257 different values. Do not use char ch.
//char ch;
//...
//while (!feof(fp)) {
// ch = fgetc(fp);
int ch;
...
while ((ch = fgetc(fp)) != EOF) {;
This is quite normal behaviour. 'malloc' just does the memory allocation, it makes no commitment on what's already in that memory location. What you probably need is 'calloc', which clears the memory and then allocates it to your program.

curious segmentation faults

I wrote a program that worked well without any errors.
I read a text file and write the contents to an array (line by line and seperat strings in this line one by one). Then I added a second textfile and again tried to read the content but then I got some crazy segmentation faults that are only reported when I debug my program.
This is the part of the program that causes the error:
vrFile = fopen("file.txt","r");
//some temporary arrays
float taz[5000], tel[5000], ttime[5000], tvr[5000][nbins], tdbz[5000][nbins];
int size = 1024, pos;
int c;
//buffer for the line to read
char *buffervr = (char *)malloc(size);
int lin=0;
if(vrFile) {
do { // read all lines in file
pos = 0;
do{ // read one line
c = fgetc(vrFile);
if(c != EOF) buffervr[pos++] = (char)c;
if(pos >= size - 1) { // increase buffer length if line is too long - leave room for 0
size *=2;
buffervr = (char*)realloc(buffervr, size);
}
}while(c != EOF && c != '\n');
buffervr[pos] = 0;
// line is now in buffer
char *ptr;
ptr = strtok(buffervr,"\t");
int abs=1;
while(ptr != NULL) {
if(abs==1){
taz[lin] = atof(ptr);
}
else if(abs==2) {
tel[lin] = atof(ptr);
}
else if (abs==3) {
ttime[lin] = atof(ptr);
}
else {
tvr[lin][abs-4]=atof(ptr);
}
abs++;
ptr = strtok(NULL, "\t");
}
lin++;
} while(c != EOF);
fclose(vrFile);
}
//free(buffervr);
The first thing is that I get a segmentation fault when I try to allocate a buffer char *buffervr = (char *)malloc(size);. When I do this before I allocate the arrays float taz[5000], tel[5000], ttime[5000], tvr[5000][nbins], tdbz[5000][nbins]; it works?!
The second thing is that I cant read the file with c = fgetc(vrFile); although I can open it. Here I get another segmentation fault?!
Can someone tell me that went wrong?
Thank you!
EDIT: actual code:
int size = 1024, pos;
int c, cdbz;
char *buffervr = (char *)malloc(size);
char *bufferdbz = (char *)malloc(size);
int lin=0;
char *taz, *tel, *ttime, **tvr, **tdbz;
taz=(char *) malloc(5000*sizeof(char));
tel=(char *) malloc(5000*sizeof(char));
ttime=(char *) malloc(5000*sizeof(char));
tvr = malloc(5000 * sizeof(char *));
int i;
for(i = 0; i < 5000; i++) {
tvr[i] = malloc(nbins * sizeof(char));
}
tdbz = malloc(5000 * sizeof(char *));
for(i = 0; i < 5000; i++) {
tdbz[i] = malloc(nbins * sizeof(char));
}
//float taz[5000], tel[5000], ttime[5000], tvr[5000][nbins], tdbz[5000][nbins];
if(vrFile!=NULL) {
do { // read all lines in file
pos = 0;
do{ // read one line
c = fgetc(vrFile);
if(c != EOF) buffervr[pos++] = (char)c;
if(pos >= size - 1) { // increase buffer length - leave room for 0
size *=2;
buffervr = (char*)realloc(buffervr, size);
}
}while(c != EOF && c != '\n');
buffervr[pos] = 0;
// line is now in buffer
char *ptr;
ptr = strtok(buffervr,"\t");
int abs=1;
while(ptr != NULL) {
if(abs==1){
taz[lin] = (ptr);
}
else if(abs==2) {
tel[lin] = (ptr);
}
else if (abs==3) {
ttime[lin] = (ptr);
}
else {
tvr[lin][abs-4]=(ptr);
}
abs++;
ptr = strtok(NULL, "\t");
}
lin++;
} while(c != EOF);
fclose(vrFile);
}
free(buffervr);
.
.
.
int lins,abss;
for (lins=0; lins<lin; lins++)
{
time[0]=ttime[lins];
az[0]=taz[lins];
el[0]=tel[lins];
for (abss=0; abss<nbins; abss++)
{
vr[abss]=tvr[lins][abss];
//dbZ[abss]=tdbz[lins][abss];
}
}
note: time, lat, lon, az, el and vr are pointers.
EDIT: I solved my Problem this way:
vrFile = fopen("file.txt","r");
if (vrFile != NULL){
while ((getline(&line, &len, vrFile)) != -1) {
NumberOfLines++;
if(NumberOfLines == 1){
line = strtok(line, "\t");
while(line != NULL){
NumberOfDoubles++;
line = strtok(NULL, "\t");
}
}
}
}
rewind(vrFile);
int i,j;
float* taz;
float* tel;
float* ttime;
float** tvr;
float** tdbz;
//1D Arrays
taz = (float*) malloc (sizeof(float)*NumberOfLines);
tel = (float*) malloc (sizeof(float)*NumberOfLines);
ttime = (float*) malloc (sizeof(float)*NumberOfLines);
//2D Arrays
tvr = (float**) malloc (sizeof(float*)*NumberOfLines);
for(i=0;i<NumberOfLines;i++){
tvr[i] = (float*) malloc (sizeof(float)*NumberOfDoubles);
}
tdbz = (float**) malloc (sizeof(float*)*NumberOfLines);
for(i=0;i<NumberOfLines;i++){
tdbz[i] = (float*) malloc (sizeof(float)*NumberOfDoubles);
}
if (vrFile != NULL){
i = 0;
while ((getline(&line, &len, vrFile)) != -1) {
line = strtok(line, "\t");
j = -3;
while(line != NULL){
switch(j){
case -3: taz[i] = strtod(line, NULL);break;
case -2: tel[i] = strtod(line, NULL);break;
case -1: ttime[i] = strtod(line, NULL);break;
default: tvr[i][j] = strtod(line, NULL);break;
}
line = strtok(NULL, "\t");
j++;
}
i++;
}
}

filling a Char array with scanf in C

How can I fill an empty Char Array with keyboard?
something like
char a_string[];
while("if not Q")
{
printf("Enter a number: ");
scanf("%c", a_string);
}
I know this is wrong
I just want to know how to give values to my a_string[], without limiting the size.
so the size will vary depend on how many keys i'm gonna enter from keyboard.
Thanks!
If you will know at the start of runtime how many keys you'll enter, you can have it ask first for the number of keys and then for the individual characters, as in the untested snippet below.
Otherwise, you have to set some real-world maximum (e.g. 10000) that will never be reached, or, if that's not possible, set a per-array maximum and make provisions for rollover into a new array. That last option really is the same (eventually bounded by memory) but gives you a larger maximum.
char *mychars;
int numchars;
printf("Please enter the total number of characters:\n");
if (scanf("%d", &numchars) == NULL) {
printf("couldn't read the input; exiting\n");
exit(EXIT_FAILURE);
}
if (numchars <= 0) {
printf("this input must be positive; exiting\n");
exit(EXIT_FAILURE);
}
mychars = (char *) malloc (numchars * sizeof(char));
int current_pos = 0;
printf("Enter a digit and hit return:\n");
while (scanf("%c", &mychars[current_pos]) != NULL && current_pos < numchars) {
current_pos++;
printf("Enter a digit and hit return:\n");
}
Try this:
#include <stdlib.h>
#include <stdio.h>
int main() {
char *string = NULL;
char *newstring = NULL;
char c = '\0';
unsigned int count = 0;
while(c != 'Q'){
c = getc(stdin);
if(string == NULL){
string = (char *) malloc(sizeof(char)); // remember to include stdlib.h
string[0] = c;
}
else{
newstring = (char *) realloc(string, sizeof(char)*count);
string = newstring;
string[count] = c;
}
count++;
}
string[count-1] = '\0'; // remove the Q character
fprintf(stderr,"here you are: %s",string);
free(string); // remember this!
return 0;
}
Repetitive calls to realloc() will meet the need.
Double realloc() size as needed to avoid O(n) calls.
char *GetQLessString(void) {
size_t size_alloc = 1;
size_t size_used = size_alloc;
char *a_string = malloc(size_alloc);
if (a_string == NULL) {
return NULL; // Out of memory
}
char ch;
while(scanf("%c", &ch) == 1 && (ch != 'Q')) {
size_used++;
if (size_used > size_alloc) {
if (size_alloc > SIZE_MAX/2) {
free(a_string);
return NULL; // Too big - been typing a long time
}
size_alloc *= 2;
char *new_str = realloc(a_string, size_alloc);
if (new_str == NULL) {
free(a_string);
return NULL; // Out of memory
}
a_string = new_str;
}
a_string[size_used - 2] = ch;
}
a_string[size_used - 1] = '\0';
return a_string;
}
Code could do a final realloc(a_string, size_used) to trim excess memory allocation.
Calling routine needs to call free() when done with the buffer.
The following would be cleaner.
int ch;
while((ch = fgetc(stdin)) != EOF && (ch != 'Q')) {

HEAP CORRUPTION DETECTED in C

I am having some problems with my program and getting this error :
HEAP CORRUPTION DETECTED: before Normal block (#9873672) at 0x00968988.
CRT detected that the application wrote to memory before start of heap buffer.
I have tried to look for fixes but I can't figure out what it wrong with my program, what to fix and where :(
Here is the function I'm using and that is causing me problems :
What I am doing is basically look into a file for a specific keyword (argument of the function getText) and printing its matching value.
Sorry if most of the variables are in French, it's a project for school and our teacher require us to use French names >_<
#include "getText.h"
#include "main.h"
#include <stdlib.h>
texteLangue* ressourcesTexteLangue = NULL;
int compteur = 0;
char* getText(char* clef)
{
char* texte = NULL;
texte = clef; //clef is the keyword passed in the function as argument
texteLangue temp;
temp.clef = clef;
texteLangue* resultat = (texteLangue*) bsearch(&temp, ressourcesTexteLangue, compteur, sizeof(texteLangue), comparerClef); //returns the value associated with the key
if (clef != NULL)
{
if (resultat != NULL)
texte = resultat->valeur;
}
return texte;
}
void lectureTexte(char* langue)
{
char nomFichierRessources[64];
sprintf(nomFichierRessources, "ressources_%s.txt", langue); //give the file name a specific ending depending on the language chosen
FILE* FichierRessources = fopen(nomFichierRessources, "r");
if (FichierRessources == NULL)
{
system("cls");
perror("The following error occured ");
system("PAUSE");
exit(42);
}
//allocates memory for the language resources
int taille = 10;
ressourcesTexteLangue = (texteLangue *) calloc(taille, sizeof(texteLangue));
if (ressourcesTexteLangue == NULL)
printf("Pas assez de place mémoire pour les ressources texte");
//gives a value to TextResource.key and TextResource.value for each line of the file
char* ligne;
while ((ligne = lectureLigne(FichierRessources)))
{
if (strlen(ligne) > 0)
{
if (compteur == taille)
{
taille += 10;
ressourcesTexteLangue = (texteLangue *) realloc(ressourcesTexteLangue, taille * sizeof(texteLangue));
}
ressourcesTexteLangue[compteur].clef = ligne;
while (*ligne != '=')
{
ligne++;
}
*ligne = '\0';
ligne++;
ressourcesTexteLangue[compteur].valeur = ligne;
compteur++;
}
}
//sorts out the values of TextResource obtained
qsort(ressourcesTexteLangue, compteur, sizeof(texteLangue), comparerClef);
fclose(FichierRessources);
}
//reads a line and returns it
char* lectureLigne(FILE *fichier)
{
int longeur = 10, i = 0, c = 0;
char* ligne = (char*) calloc(longeur, sizeof(char));
if (fichier)
{
c = fgetc(fichier);
while (c != EOF)
{
if (i == longeur)
{
longeur += 10;
ligne = (char*) realloc(ligne, longeur * sizeof(char));
}
ligne[i++] = c;
c = fgetc(fichier);
if ((c == '\n') || (c == '\r'))
break;
}
ligne[i] = '\0';
while ((c == '\n') || (c == '\r'))
c = fgetc(fichier);
if (c != EOF)
ungetc(c,fichier);
if ((strlen(ligne) == 0) && (c == EOF))
{
free(ligne);
ligne = NULL;
}
}
return ligne;
}
//frees the TextRessources
void libererTexte()
{
if (ressourcesTexteLangue != NULL)
{
while (compteur--)
{
free(ressourcesTexteLangue[compteur].clef);
}
free(ressourcesTexteLangue);
}
}
//compares the keys
int comparerClef(const void* e1, const void* e2)
{
return strcmp(((texteLangue*) e1)->clef, ((texteLangue*) e2)->clef);
}
the structure of RessourceTextelangue (TextResources) look like this :
typedef struct texteLangue {
char* clef;
char* valeur;
} texteLangue;
There are several potential problems with your code that could be causing the error report you see.
Here is one:
if (i == longeur)
should be:
if ((i+1) == longeur)
otherwise,
ligne[i] = '\0';
can occur in conditions when
ligne[i++] = c;
has caused i to become equal to longeur.
Here is another:
while (*ligne != '=')
{
ligne++;
}
*ligne = '\0';
the above code should be:
while (*ligne != '=' && *ligne != '\0')
{
ligne++;
}
*ligne = '\0';
otherwise, you will corrupt memory in the case when there is no '=' to be found in the string.
Although either of these could cause the symptom you report, I see some other oddities that make me think there is more wrong than I have seen so far. Nevertheless, fixing those two problems will at least reduce the number of possibilities you have to consider.
Is your input guaranteed to contain a '=' in each line?
while (*ligne != '=') // if not, this will fly off the end of your buffer...
{
ligne++;
}
*ligne = '\0'; // ...and write to unallocated heap memory
Edit
Given #Heath's comment, if your input contains a blank line (including ending with a single blank line) then the above would most certainly be triggered.
c = fgetc(fichier); // reads '\n'
while (c != EOF)
{
...
ligne[i++] = c;
...
ligne[i] = '\0';
ligne now contains "\n" and is returned. later it is used:
if (strlen(ligne) > 0) // strlen("\n") is greater than 0
{
...
while (*ligne != '=') // oops! loop until we find a '=' somewhere
// in the heap or crash trying.
{
ligne++;
}
*ligne = '\0'; // corrupt the heap here

fgetc read file line by line

So I'm working on a function that will use fgetc to read a line into a buffer. so I can use that buffer as I please, and then refill the buffer with the next line. My function works however I have to repeat code outside of the for loop to process the last line as shown here:
for(i = 0, c = 1; ch != EOF; i++)
{
ch = fgetc(grab);
if(ch == 0x0A)
{
/*Process Line*/
c = 1;
}
else
{
linetmp = realloc(line, (c + 1) * sizeof(char));
if(!linetmp)
{
free(line);
free(url);
printf("\nError! Memory allocation failed!");
return 1;
}
line = linetmp;
line[c - 1] = ch;
line[c] = 0x00;
c++;
}
}
/*repeat if(ch == 0x0A) statement*/
I would rather do this all in the same loop but am not sure on how I would go about doing this. Any help would be greatly appreciated!
I would recommend that you instead use getline() if you're on a POSIX system.
Also, your logic is strange since you check for EOF in the loop header only, but update ch inside the loop. That means it will run through with ch == EOF, before the loop condition is re-evaluated.
You should try putting the updating and the check together, making the loop header read like this:
for(i = 0, c = 1; (ch = fgetc()) != EOF; i++)
Also, you need to think about line separators, both '\n' (carriage return) and '\n' (line feed) can occur.
I don't think you should reallocate after each character. If you want to have the buffer at the smallest value needed, you could reallocate at the end with ( strlen() + 1); Also, there is a function fgets() which reads a line.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int somefunc(FILE *grab)
{
int current_size = 100;
int data_size = current_size - 1;
char *url = malloc(current_size);
char *line = malloc(current_size);
char *linetmp;
int ch;
ch = fgetc(grab);
int i = 0;
int c = 0;
while (ch != EOF && ch != 0x0A )
{
i++;
if ( i > data_size )
{
current_size = current_size * 2;
data_size = current_size - 1;
linetmp = realloc(line, current_size);
if (!linetmp)
{
free(line);
free(url);
printf("\nError! Memory allocation failed!");
return 1;
}
line = linetmp;
}
line[c] = ch;
c++;
ch = fgetc(grab);
}
line[c] = '\0';
linetmp = realloc(line,strlen(line) + 1);
line = linetmp;
printf("we just read line->%s\n",line);
free(line);
free(url);
return 0;
}
int main(void)
{
char *cpFilename = "somefile.txt";
FILE *fp = fopen(cpFilename,"r");
if ( fp == NULL )
{
printf("ERROR: could not open %s\n",cpFilename);
printf("Error code: %d\n",errno);
perror("ERROR:");
return 1;
}
int return_code = somefunc(fp);
while (return_code != EOF && return_code != 1)
{
return_code = somefunc(fp);
}
fclose(fp);
}

Resources