WHEN I TRY TO OPEN FILE IN APPEND MODE ,IT CREATES THE FILE BUT IT SAYS "IN-VALID ENCODING " ALONG WITH THE FILE-NAME ........I HAVE PASSED THE ARRAY OF STRINGS CORRECTLY IN string moreover the strings in "string" are sorted .....any string in "string" is made up of only 4 characters "A","G","T","C".

#include <malloc.h>

#include"check_duplication_write.h"

int check_duplication_write(int *no_of_strings,char *file_name,char **string){
    char *dest;
    int i,result;
    int temp_length=LENGTH-FIRSTCHARACTERS;//no need of writing a null character in the file
    char temp[temp_length];

    dest=(char *)malloc(1024*1024*1024);
    char result_files[FIRSTCHARACTERS+5];

    for(i=0;i<(*no_of_strings-1);++i){

        if(!strncmp(string[i],string[i+1],FIRSTCHARACTERS)){
            //the two adjacent strings have first four characters same

            if(strcmp(string[i],string[i+1])){
                //the two adjacent string are not exactly same

                strncpy(temp,string[i]+FIRSTCHARACTERS,temp_length);
                strcat(dest,temp);
                }

                
                
            }else{}//bypass if we find adjacent string exactly same

        }else{
            
            //writing to the file should be done here.
            strcpy(result_files,"");
            strncpy(result_files,string[i],FIRSTCHARACTERS);
            //strcat(result_files,"\0");
            strcat(result_files,".txt");

            FILE *intermediate_files=fopen(result_files,"a");
            fwrite(dest,strlen(dest),1,intermediate_files);
            fclose(intermediate_files);
            free(dest);
            dest=(char *)malloc(1024*1024*1024);
        }
    }
    free(dest);
    return result;
}

Recommended Answers

All 5 Replies

A few suggestions:

malloc() is defined in the include file "alloc.h", not "malloc.h". ;)

Without the stdio.h include file, I'm not surprised you're having problems opening files and such. ;)

After you malloc() for an array (especially after such a large one), be sure to check the returned pointer address to be sure the request succeeded, and is not NULL.

btw, the return addy from malloc() doesn't need to be cast, 99.9% of the time.

I beg to differ. malloc is defined in some implementation-defined library and declared in <stdlib.h>.

I beg to differ. malloc is defined in some implementation-defined library and declared in <stdlib.h>.

Well, I figured he was going for "alloc.h", since he's just one letter off. Either header file (alloc.h or stdlib.h), will have the needed declarations:


malloc Allocates memory.

Syntax:
void *malloc(size_t size);

Prototype in:
alloc.h stdlib.h

Remarks:
malloc allocates a block of size bytes from the memory heap. It allows a
program to allocate memory explicitly as it's needed, and in the exact
amounts needed.

The heap is used for dynamic allocation of variable-sized blocks of memory.
Many data structures, such as trees and lists, naturally employ heap memory
allocation.

All the space between the end of the data segment and the top of the program
stack is available for use in the small data models, except for a small
margin immediately before the top of the stack.

This margin is intended to allow the application some room to make the stack
larger, in addition to a small amount needed by DOS.

In the large data models, all the space beyond the program stack to the end
of available memory is available for the heap.

Return Value:
On success, malloc returns a pointer to the newly allocated block of memory.

If not enough space exists for the new block, it returns null. The contents
of the block are left unchanged.

If the argument size == 0, malloc returns null.

Portability:
malloc is available on UNIX systems and is defined in ANSI C.

Example:

#include <stdio.h>
 #include <string.h>
 #include <alloc.h>    //or <stdlib.h>
 #include <process.h>

 int main(void)
 {
    char *str;

    /* allocate memory for string */
    if ((str = malloc(10)) == NULL)
    {
       printf("Not enough memory to allocate buffer\n");
       exit(1);  /* terminate program if out of memory */
    }

    /* copy "Hello" into string */
    strcpy(str, "Hello");

    /* display string */
    printf("String is %s\n", str);

    /* free memory */
    free(str);

    return 0;
 }

Only <stdlib.h> exists in any form of the C standard, or on my system.

% cat file.c 
#include <alloc.h>
#include <stdio.h>

int main(void)
{
        char *x = malloc(3);
        x[0]='A';
        x[1]='\n';
        x[2]=0;
        printf("%s", x);
        return 0;
}
% cc file.c 
file.c:1:19: error: alloc.h: No such file or directory
file.c: In function ‘main’:
file.c:5: warning: incompatible implicit declaration of built-in function ‘malloc’

Don't encourage people to use nonstandard headers.

>Don't encourage people to use nonstandard headers.
Indeed, especially when the nonstandard header is also completely unnecessary.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.