Example #1
2
static void
initFiles(void)
{
    char buf[BUFSIZ];

    snprintf(buf,BUFSIZ,"%s%s_%d.json", dirpath, prefix, queryid);
    tachojson= fopen(buf,"w");
    if( tachojson == NULL) {
        fprintf(stderr,"Could not create %s\n",buf);
        exit(-1);
    }
    snprintf(buf,BUFSIZ,"%s%s_%d_mal.csv",dirpath, prefix, queryid);
    tachomal= fopen(buf,"w");
    if( tachomal == NULL) {
        fprintf(stderr,"Could not create %s\n",buf);
        exit(-1);
    }
    snprintf(buf,BUFSIZ,"%s%s_%d_stmt.csv", dirpath, prefix, queryid);
    tachostmt= fopen(buf,"w");
    if( tachostmt == NULL) {
        fprintf(stderr,"Could not create %s\n",buf);
        exit(-1);
    }
    snprintf(buf,BUFSIZ,"%s%s_%d.trace", dirpath, prefix, queryid);
    tachotrace= fopen(buf,"w");
    if( tachotrace == NULL) {
        fprintf(stderr,"Could not create %s\n",buf);
        exit(-1);
    }
}
Example #2
2
int main( int argc, char *argv[ ] ) {
    FILE *in, *out;

    in = fopen( "input.dat", "r" );
    if ( in == NULL ) {
        perror( "input.dat" );
        return 1;
    }
    out = fopen( "output.dat", "w" );
    if ( out == NULL ) {
        perror( "output.dat" );
        return 1;
    }
    int x, p, n, y, rc;
    while ( fscanf( in, "%x %d %d %x", &x, &p, &n, &y ) != EOF ) {
        rc = setbits( x, p, n, y );
        fprintf( out, "setbits(0x%x, %d, %d, 0x%x) = 0x%x\n", x, p, n, y, rc );
        printf( "0x1234 | 0x0000 = %x\n", x | 0 );
        printf( "0x1234 | 0x1111 = %x\n", x | ~0 );
    }

    fclose( in );
    fclose( out );

    return 0;
}
Example #3
1
static int cmpfile(const char *str, const char *path, const char *fname)
{
    char filename[1024], *buffer;
    int ret;
    FILE *file;

    sprintf(filename, "%s%c%s", path, dir_sep, fname);
    file = fopen(filename, "rb");
    if (!file) {
        if (conf.strip)
            strcat(filename, ".strip");
        else
            strcat(filename, ".normal");
        file = fopen(filename, "rb");
    }
    if (!file) {
        printf("Error: test result file could not be opened.\n");
        exit(1);
    }

    buffer = loadfile(file);
    if (strcmp(buffer, str) != 0)
        ret = 1;
    else
        ret = 0;
    free(buffer);
    fclose(file);

    return ret;
}
Example #4
1
int filecopy( char *oldfilename, char *newfilename)
{
    FILE *oldfile;
    FILE *newfile;
    char c_temp;

    if( (oldfile = fopen( oldfilename, "r") ) == NULL ) {
        return -1;
    }
    if( (newfile = fopen( newfilename, "w") ) == NULL ) {
        return -1;
    }

    while(1) {
        c_temp = fgetc( oldfile);
        if( c_temp == EOF)
            break;
        fputc( c_temp, newfile);
    };

    fclose( oldfile);
    fclose( newfile);

    return 0;
}
Example #5
1
int main(int argc, char** args) {
    FILE* instruction_file = fopen(args[1], "r");
    char** instructions = NULL;
    int lines = get_instructions(instruction_file, &instructions);
    fclose(instruction_file);

    var** input_variables = NULL;
    int num_input_variables = get_input_variables(instructions, lines, &input_variables);

    var** temp_variables = NULL;
    int num_temp_variables = get_temporary_variables(instructions, lines, &temp_variables);

    var** output_variables = NULL;
    int num_output_variables = get_output_variables(instructions, lines, &output_variables);

    FILE* value_file = fopen(args[2], "r");
    char* string = (char *) malloc(256 * sizeof(char *));
    while (fscanf(value_file, "%[^\n]\n", string) == 1) {
        int i;
        for (i = 0; i < num_input_variables; i++) {
            input_variables[i] -> value = string[i*2] - '0';
        }
        run_program(instructions, lines,
                    input_variables, num_input_variables,
                    temp_variables, num_temp_variables,
                    output_variables, num_output_variables
                   );
    }
    return 0;
}
Example #6
1
File: io.c Project: qingl97/ENSEIRB
/*************************************************************************
* This function writes out the partition vectors for a mesh
**************************************************************************/
void WriteMeshPartition(char *fname, int nparts, int ne, idxtype *epart, int nn, idxtype *npart)
{
    FILE *fpout;
    int i;
    char filename[256];

    sprintf(filename,"%s.epart.%d",fname, nparts);

    if ((fpout = fopen(filename, "w")) == NULL)
        errexit("Problems in opening the partition file: %s", filename);

    for (i=0; i<ne; i++)
        fprintf(fpout,"%d\n", epart[i]);

    fclose(fpout);

    sprintf(filename,"%s.npart.%d",fname, nparts);

    if ((fpout = fopen(filename, "w")) == NULL)
        errexit("Problems in opening the partition file: %s", filename);

    for (i=0; i<nn; i++)
        fprintf(fpout,"%d\n", npart[i]);

    fclose(fpout);


}
Example #7
1
int main( int argc, char* argv[] )
{
    char *infile = argv[1];
    char *otfile = argv[2];

    FILE *in = fopen( infile, "r" );
    FILE *ot = fopen( otfile, "w" );

    int res;
    int num;
    int total = 0;
    int values = 0;

    res = fread( &num, sizeof(int), 1, in );
    while( res > 0 )
    {
        total += num;
        values += 1;

        fprintf( ot, "%u\n", num );
        res = fread( &num, sizeof(int), 1, in );
    }

    float avg = (float)total / (float)values;
    printf( "Average: %0.3f\n", avg );

    fclose( in );
    fclose( ot );

    return 0;
}
Example #8
0
int main(int argc,char *argv[])
{
    FILE *in,*out;
    int ch;
    char name[LEN];
    int count = 0;

    if(argc<2)
    {
        fprintf(stderr,"useage %s filename.\n",argv[0]);
        exit(1);
    }
    if((in = fopen(argv[1],"r"))==NULL)
    {
        fprintf(stderr,"I couldn't open the file %s\n",argv[1]);
        exit(2);
    }
    strcpy(name,argv[1]);
    strcat(name,".red");
    if((out = fopen(name,"w"))==NULL)
    {
        fprintf(stderr,"Can't create output file.\n");
        exit(3);
    }
    while((ch = getc(in))!=EOF)
    {
        if(count++%3==0)putc(ch,out);
    }
    if(fclose(in)!=0||fclose(out)!=0)
        fprintf(stderr,"Error in closeing files.\n");
    return 0;
}
Example #9
0
void
ICSInitScript ()
{
    /* try to open the icsLogon script, either in the location given
     * or in the users HOME directory
     */

    FILE *f;
    char buf[MSG_SIZ];
    char *homedir;

    f = fopen(appData.icsLogon, "r");
    if (f == NULL)
    {
        homedir = getenv("HOME");
        if (homedir != NULL)
        {
            safeStrCpy(buf, homedir, sizeof(buf)/sizeof(buf[0]) );
            strncat(buf, "/", MSG_SIZ - strlen(buf) - 1);
            strncat(buf, appData.icsLogon,  MSG_SIZ - strlen(buf) - 1);
            f = fopen(buf, "r");
        }
    }

    if (f != NULL)
        ProcessICSInitScript(f);
    else
        printf("Warning: Couldn't open icsLogon file (checked %s and %s).\n", appData.icsLogon, buf);

    return;
}
static void open_files(prescanner_t* prescanner)
{
    if (strcmp(prescanner->input_filename, "-") != 0)
    {
        prescanner->input_file = fopen(prescanner->input_filename, "r");

        if (prescanner->input_file == NULL)
        {
            running_error("Cannot open input file '%s' (%s)\n", prescanner->input_filename,
                          strerror(errno));
        }
    }
    else
    {
        prescanner->input_file = stdin;
        prescanner->input_filename = "(stdin)";
    }

    if (strcmp(prescanner->output_filename, "-") != 0)
    {
        prescanner->output_file = fopen(prescanner->output_filename, "w");

        if (prescanner->output_file == NULL)
        {
            running_error("Cannot open output file '%s' (%s)\n", prescanner->output_filename,
                          strerror(errno));
        }
    }
    else
    {
        prescanner->output_file = stdout;
        prescanner->output_filename = "(stdout)";
    }
}
Example #11
0
File: 2cca.c Project: randunel/2cca
/*
 * Load CA certificate and private key from current dir
 */
static int load_ca(char * ca_name, identity * ca)
{
    FILE * f ;
    RSA  * rsa ;
    char filename[FIELD_SZ+1] ;

    sprintf(filename, "%s.crt", ca_name);
    if ((f=fopen(filename, "r"))==NULL) {
        fprintf(stderr, "Cannot find: %s\n", filename);
        return -1 ;
    }
    ca->cert = PEM_read_X509(f, NULL, NULL, NULL);
    fclose(f);

    sprintf(filename, "%s.key", ca_name);
    if ((f=fopen(filename, "r"))==NULL) {
        return -1 ;
    }
    rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL);
    fclose(f);

    ca->key = EVP_PKEY_new();
    EVP_PKEY_assign_RSA(ca->key, rsa);

    if (!X509_check_private_key(ca->cert, ca->key)) {
        fprintf(stderr, "CA certificate and private key do not match\n");
        return -1 ;
    }
    return 0;
}
Example #12
0
File: main.cpp Project: CCJY/coliru
int main( void )
{
    FILE *stream;
    char list[30];
    int  i, numread, numwritten;

    // Open file in text mode:
    stream = fopen( "fread.out", "w+t" );
    if( stream )
    {
        for ( i = 0; i < 25; i++ )
            list[i] = (char)('z' - i);
        // Write 25 characters to stream
        numwritten = fwrite( list, sizeof( char ), 25, stream );
        printf( "Wrote %d items\n", numwritten );
        fclose( stream );

    }
    else
        printf( "Problem opening the file\n" );

    stream = fopen("fread.out", "r+t");
    if( stream )
    {
        // Attempt to read in 25 characters
        numread = fread( list, sizeof( char ), 25, stream );
        printf( "Number of items read = %d\n", numread );
        printf( "Contents of buffer = %.25s\n", list );
        fclose( stream );
    }
    else
        printf( "File could not be opened\n" );
}
Example #13
0
void FunctionAddressRegistry::dumpPerfMap() {
    std::string out_path = "perf_map";
    removeDirectoryIfExists(out_path);

    llvm_error_code code;
    code = llvm::sys::fs::create_directory(out_path, false);
    assert(!code);

    FILE* index_f = fopen((out_path + "/index.txt").c_str(), "w");

    char buf[80];
    snprintf(buf, 80, "/tmp/perf-%d.map", getpid());
    FILE* f = fopen(buf, "w");
    for (const auto& p : functions) {
        const FuncInfo& info = p.second;
        fprintf(f, "%lx %x %s\n", (uintptr_t)p.first, info.length, info.name.c_str());

        if (info.length > 0) {
            fprintf(index_f, "%lx %s\n", (uintptr_t)p.first, info.name.c_str());

            FILE* data_f = fopen((out_path + "/" + info.name).c_str(), "wb");

            int written = fwrite((void*)p.first, 1, info.length, data_f);
            assert(written == info.length);
            fclose(data_f);
        }
    }
    fclose(f);
}
Example #14
0
int fileresult_save(struct _gfxresult*gfx, const char*filename)
{
    gfxresult_internal_t*i = (gfxresult_internal_t*)gfx->internal;
    FILE*fi,*fo;
    fi = fopen(i->filename, "rb");
    if(!fi) {
        perror(i->filename);
        return 0;
    }
    fo = fopen(filename, "wb");
    if(!fo) {
        perror(filename);
        return 0;
    }

    char buf[4096];
    while(!feof(fi)) {
        int l = fread(buf, 1, 4096, fi);
        if(l>0) {
            fwrite(buf, 1, l, fo);
        } else {
            break;
        }
    }

    fclose(fi);
    fclose(fo);
    return 0;
}
void openFiles(char *tokenName, char *listingName, char *fileName, char *addressesName) {
    tokenFile = fopen(tokenName, "w");
    if(tokenFile == NULL) {
        printf("Cannot open token file, exiting\n");
        exit(1);
    }
    fprintf(tokenFile, "Line# Lexeme      Token Type  Attribute\n");
    listingFile = fopen(listingName, "w");
    if(listingFile == NULL) {
        printf("Cannot open listing file, exiting\n");
        exit(1);
    }
    srcFile = fopen(fileName, "r");
    if(srcFile == NULL) {
        printf("Cannot open source file, exiting\n");
        exit(1);
    }
    srcLineFile = fopen(fileName, "r");
    if(srcFile == NULL) {
        printf("Cannot open source file, exiting\n");
        exit(1);
    }
    addressesFile = fopen(addressesName, "w");
    if(addressesFile == NULL) {
        printf("Cannot open addresses file, exiting\n");
        exit(1);
    }
    char buffer[74];
    fgets(buffer, 73, srcLineFile);
    fprintf(listingFile, "%-8d%s", 1, buffer);
}
Example #16
0
bool StaticMapTree::CanLoadMap(const std::string &vmapPath, uint32 mapID, uint32 tileX, uint32 tileY)
{
    std::string basePath = vmapPath;
    if (basePath.length() > 0 && (basePath[basePath.length()-1] != '/' && basePath[basePath.length()-1] != '\\'))
        basePath.append("/");
    std::string fullname = basePath + VMapManager2::getMapFileName(mapID);
    bool success = true;
    FILE *rf = fopen(fullname.c_str(), "rb");
    if (!rf)
        return false;
    // TODO: check magic number when implemented...
    char tiled;
    char chunk[8];
    if (!readChunk(rf, chunk, VMAP_MAGIC, 8) || fread(&tiled, sizeof(char), 1, rf) != 1)
    {
        fclose(rf);
        return false;
    }
    if (tiled)
    {
        std::string tilefile = basePath + getTileFileName(mapID, tileX, tileY);
        FILE* tf = fopen(tilefile.c_str(), "rb");
        if (!tf)
            success = false;
        else
        {
            if (!readChunk(tf, chunk, VMAP_MAGIC, 8))
                success = false;
            fclose(tf);
        }
    }
    fclose(rf);
    return success;
}
Example #17
0
static void v4lcontrol_get_dmi_string(const char *sysfs_prefix, const char *string, char *buf, int size)
{
    FILE *f;
    char *s, sysfs_name[512];

    snprintf(sysfs_name, sizeof(sysfs_name),
             "%s/sys/class/dmi/id/%s", sysfs_prefix, string);
    f = fopen(sysfs_name, "r");
    if (!f) {
        /* Try again with a different sysfs path, not sure if this is needed
           but we used to look under /sys/devices/virtual/dmi/id in older
           libv4l versions, but this did not work with some kernels */
        snprintf(sysfs_name, sizeof(sysfs_name),
                 "%s/sys/devices/virtual/dmi/id/%s", sysfs_prefix, string);
        f = fopen(sysfs_name, "r");
        if (!f) {
            buf[0] = 0;
            return;
        }
    }

    s = fgets(buf, size, f);
    if (s)
        s[strlen(s) - 1] = 0;
    fclose(f);
}
Example #18
0
int main(int argc, char *argv[])
{
    if (argc != 4)
        return EXIT_FAILURE;

    FILE *in = fopen(argv[1], "rb");
    FILE *out = fopen(argv[3], "wb");
    if (!in || !out)
        return EXIT_FAILURE;

    fprintf(out,
            "#include <vector>\n"
            "namespace digidoc {\n"
            " std::vector<unsigned char> %s() {\n"
            "  static const unsigned char data[] = {", argv[2]);
    int ch;
    for (int i = 0; (ch = fgetc(in)) != EOF; ++i)
    {
        if (i % 12 == 0)
            fprintf(out, "\n");
        fprintf(out, " %#04x,", ch);
    }
    fprintf(out,
            " 0x00\n"
            "  };\n"
            "  return std::vector<unsigned char>(data, data + sizeof(data));\n"
            " }\n"
            "}\n");
    fclose(in);
    fclose(out);

    return EXIT_SUCCESS;
}
Example #19
0
static FILE *
i_open(char *name, char **dirs, int ndirs)
{
    /* first try curent directory, then directories in -I options */
    FILE *in = fopen(name, "r");
    if (!in) {
        char tmp[2048];
        long i, n, len = strlen(name);
        for (i=ndirs-1 ; i>=0 && !in ; i--) {  /* scan -I in reverse order */
            n = strlen(dirs[i]);
            if (n+len > 2046) {
                E_("codger: pkg.i name plus -Idir option >2046 characters");
                return 0;
            }
            strcpy(tmp, dirs[i]);
            if (n && tmp[n-1]!='/') strcpy(tmp+n, "/");
            strcat(tmp+n, name);
            in = fopen(tmp, "r");
        }
        if (!in)
            fprintf(stderr, "codger: unable to find %s, need -I?", name);
        else
            fprintf(stdout, "found %s in %s\n", name, dirs[i+1]);
    } else {
        fprintf(stdout, "found %s in current directory\n", name);
    }
    return in;
}
Example #20
0
/*
 * methlabs.org used to provide at http://methlabs.org/sync/ some address lists
 * in the following multi line format
 *
 * Some random junk:aaa.bbb.ccc.ddd-eee.fff.ggg.hhh
 *
 * where aaa.bbb.ccc.ddd-eee.fff.ggg.hhh represents a continuous range of
 * IPv4 addresses. Those ranges are not always a CIDR network so we may have
 * to split them  in a set of netmaskable entities. Also both ends of those
 * ranges may be identical, thus describing a single address.
 *
 * We convert that to a list of ipaddress/netmask, one per line,
 * suitable to use with OpenBSD's pfctl and doubtless other packages.
 *
 * This tool is written in a very portable way so should compile
 * and work on any system having an C89 compiler (and a strdup()
 * implementation).
 *
 * Comments may be sent to me at <bruno@rohee.org>.
 */
int main(int argc, char **argv) {
    FILE *in;
    FILE *out;

    if(argc != 3) {
        fprintf(stderr, "usage: %s infile outfile\n", argv[0]);
        return EXIT_FAILURE;
    }

    in = fopen(argv[1], "ro");
    out = fopen(argv[2], "w");

    convert(in, out);

    fclose(in);
    fclose(out);

#ifdef GATHER_STATS
    printf("Parsed %d lines, of which %d defined a single IP address,\n"
           "%d defined a CIDR network and %d defined a more complex range.\n",
           lineCount, singleAddressCount,
           networkAddressCount, complexRangeCount);
#endif

    return EXIT_SUCCESS;
}
Example #21
0
void main() {
    FILE *fs = fopen(filename, "r");
    if(fs == NULL) {
        printf("File opening failed\n");
        exit(0);
    }

    printf("FILE opened\n");

    int row_count = 0;
    char buf[MAX_BUF_SIZE];
    while(fgets(buf, MAX_BUF_SIZE, fs) != NULL) {
        printf("reading: %s\n", buf);
        row_count++;
        if(buf[0] != '\0') {
            process(buf);
        }
    }

    printf("ROW processed: %d\n", row_count);
    fclose(fs);

    FILE *ft = fopen(file_out, "w");
    if(ft == NULL) {
        printf("FILE creation failed\n");
        exit(0);
    }
    write_out(ft);

    fclose(ft);

}
Example #22
0
/* Open a file. We take an optional fallback dir as well and will try opening
 * there if opening directly fails.
 *
 * This is used for things like finding ICC profiles. We try to open the file
 * directly first, and if that fails and the filename does not contain a
 * directory separator, we try looking in the fallback dir.
 */
FILE *
vips__file_open_read( const char *filename, const char *fallback_dir,
                      gboolean text_mode )
{
    char *mode;
    FILE *fp;

#ifdef BINARY_OPEN
    if( text_mode )
        mode = "r";
    else
        mode = "rb";
#else /*BINARY_OPEN*/
    mode = "r";
#endif /*BINARY_OPEN*/

    if( (fp = fopen( filename, mode )) )
        return( fp );

    if( fallback_dir && !filename_hasdir( filename ) ) {
        char *path;

        path = g_build_filename( fallback_dir, filename, NULL );
        fp = fopen( path, mode );
        g_free( path );

        if( fp )
            return( fp );
    }

    vips_error( "vips__file_open_read",
                _( "unable to open file \"%s\" for reading" ), filename );

    return( NULL );
}
Example #23
0
int main ( int argc, char* argv[])
{

    FILE* mfold = fopen(argv[1],"r");                                           //File with numbers for sorting.
    FILE* mfsort = fopen(argv[2],"w+");                                         //New file with sorting numbers.
    int i = 0;                                                                  //Counter
    int* mass = (int*)malloc(BUF*sizeof(int));                                  //An array with sortable elements.
    int N = BUF;
    while (!feof(mfold)) {                                                      //Create an array from first file.
        if (i==N) {
            mass = (int*)malloc((N+BUF)*sizeof(int));                    //
            N+=BUF;                                                                 //
        }                                                                       //
        fscanf(mfold,"%d",&mass[i]);                                            //
        i++;                                                                    //
    }                                                                           //

    if (i == 1) {                                                               //Check, is the file is empty. If true, then exit.
        fprintf(stderr,"%s","File is empty!\n");                                //
        exit(0);                                                                //
    }                                                                           //

    N = i-1;                                                                    //N - the number of elements.
    QSort (mass,0,N-1);                                                         //Sort an array.

    for(i = 0; i < N; i++) {                                                    //Fill new file with sorting numbers from our array.
        fprintf(mfsort,"%d ",mass[i]);                                          //
    }                                                                           //

    free(mass);                                                                 // Free the memory.
    return 0;
}
void strmrep(const char* filename, const char* key)
{
    FILE* input = fopen(filename, "r");

    /* Let's write out immediately, then rename when we're done. */
    FILE* output = fopen("output.txt", "w");

    StartResult result;
    unsigned int matches = 0;

    while((result = strmstart(key, input, output)) == MATCH || result == NO_MATCH)
    {
        if(result == MATCH)
        {
            fprintf(output, "(%d)", ++matches);
        }

        if(fpeek(input) == EOF)
        {
            break;
        }
    }

    fclose(input);
    fclose(output);

    rename("output.txt", filename);
}
void kodirane()
{
    unsigned short int j, c;
    FILE *input, *output;
    input=fopen("input.txt","r");
    output=fopen("output.txt","w");
    while(!feof(input))
    {
        fread(&current_letter, sizeof(unsigned char), 1, input);
        if(kod_dekod==0)
            current_letter=(unsigned char)rotors[left_rotor][(left_rotor_offset+rotors[center_rotor][(center_rotor_offset+rotors[right_rotor][(right_rotor_offset+plugboard[(int)current_letter-65])%26])%26])%26]+65;
        else
        {
            c=(unsigned short int)current_letter-65;
            for(j=0; rotors[left_rotor][(left_rotor_offset+j)%26]!=c; j++);
            c=j;
            for(j=0; rotors[center_rotor][(center_rotor_offset+j)%26]!=c; j++);
            c=j;
            for(j=0; rotors[right_rotor][(right_rotor_offset+j)%26]!=c; j++);
            c=j;
            for(j=0; plugboard[j]!=c; j++);
            c=j;
            current_letter=(unsigned char)c+65;
        }
        left_rotor_offset++;
        left_rotor_offset%=26;
        center_rotor_offset++;
        center_rotor_offset%=26;
        right_rotor_offset++;
        right_rotor_offset%=26;
        fwrite(&current_letter, sizeof(unsigned char), 1, output);
    }
    fclose(input);
    fclose(output);
}
Example #26
0
int main(int argc, char **argv) {

    if(argc < 3) {
        fprintf(stderr, "Parameters Lost\n");
        return 0;
    }

    FILE *fin = fopen(argv[1], "r"), *fout = fopen(argv[2], "w");
    int tmp, res = 0;

    if(fin == NULL) {
        fprintf(stderr, "Fail to Access Input File\n");
        return 0;
    }

    while( ( tmp = getc(fin) ) != EOF ) {
        if(tmp == 'a' || tmp == 'e' || tmp == 'i' || tmp == 'o' || tmp == 'u')	res++;
        if(tmp == 'A' || tmp == 'E' || tmp == 'I' || tmp == 'O' || tmp == 'U')	res++;
    }

    fprintf(fout, "%d", res);

    fclose(fin);
    fclose(fout);

    return 0;

}
Example #27
0
int loadCharPoolPetOne( char *id , char *output , int outlen)
{
    char fn[256];
    FILE *fp;

    char c_temp,*c_ptr;
    int sizes=0;
    c_ptr=output;

    makeCharPoolPetFileName( id , fn , sizeof(fn));

    fp  = fopen( fn , "r" );
    if( fp == NULL ) {
        if( (fp = fopen( fn , "w" )) == NULL)
            return -1;
        return 0;
    }

    do {
        c_temp = fgetc(fp);
        *c_ptr=c_temp;
        c_ptr++;
        sizes++;
        if( sizes >= (outlen-1) ) break;
    } while(c_temp != EOF);
    *c_ptr='\0';
    fclose(fp);
    return sizes;
}
Example #28
0
struct t_upgrade_file *
upgrade_file_new (const char *filename, int write)
{
    int length;
    struct t_upgrade_file *new_upgrade_file;

    if (!filename)
        return NULL;

    new_upgrade_file = malloc (sizeof (*new_upgrade_file));
    if (new_upgrade_file)
    {
        /* build name of file */
        length = strlen (weechat_home) + 1 + strlen (filename) + 16 + 1;
        new_upgrade_file->filename = malloc (length);
        if (!new_upgrade_file->filename)
        {
            free (new_upgrade_file);
            return NULL;
        }
        snprintf (new_upgrade_file->filename, length, "%s/%s.upgrade",
                  weechat_home, filename);

        /* open file in read or write mode */
        if (write)
            new_upgrade_file->file = fopen (new_upgrade_file->filename, "wb");
        else
            new_upgrade_file->file = fopen (new_upgrade_file->filename, "rb");

        if (!new_upgrade_file->file)
        {
            free (new_upgrade_file->filename);
            free (new_upgrade_file);
            return NULL;
        }

        /* change permissions if write mode */
        if (write)
        {
            chmod (new_upgrade_file->filename, 0600);

            /* write signature */
            upgrade_file_write_string (new_upgrade_file, UPGRADE_SIGNATURE);
        }

        /* init positions */
        new_upgrade_file->last_read_pos = 0;
        new_upgrade_file->last_read_length = 0;

        /* add upgrade file to list of upgrade files */
        new_upgrade_file->prev_upgrade = last_upgrade_file;
        new_upgrade_file->next_upgrade = NULL;
        if (upgrade_files)
            last_upgrade_file->next_upgrade = new_upgrade_file;
        else
            upgrade_files = new_upgrade_file;
        last_upgrade_file = new_upgrade_file;
    }
    return new_upgrade_file;
}
Example #29
0
int main(int argc, char ** argv)
{
    // Открываем файлы и считываем данные
    FILE *fin, *fout;
    fin = fopen("numbers.txt", "r");
    fout = fopen("output.txt", "w");
    if(fin == NULL || fout == NULL)
    {
        printf("Error!");
        return 1;
    }

    int n, i;
    fscanf(fin, "%d", &n);
    int * A = (int *)malloc(n * sizeof(int));
    for ( i = 0; i < n; ++i )
    {
        fscanf(fin, "%d", &A[i]);
    }
    fclose(fin);

    // Сортируем

    radix_sort(A, n);

    // Записывае данные и закрываем файлы

    for (int i = 0; i < n; ++i)
        fprintf(fout, "%d ", A[i]);

    fclose(fout);
    free(A);
    return 0;
}
Example #30
0
File: sss.c Project: srth12/network
bool signup(char uname[50],char passwd[3],char str[50]) {

    char i[50],j[50],line[50];
    FILE *fp;
    fp=fopen("users.txt","r");
    if(fp==NULL) {
        printf("File open failed\n");
        exit(0);
    }
    while(fgets(line,50,fp) !=NULL) {
        //printf("[:%s:]\n",line);
        sscanf(line,"%s%s",i,j);
        if(strncmp(uname,i,7)==0) {
            return false;
        }
    }

    fclose(fp);

    fp=fopen("users.txt","a");
    if(fp==NULL) {
        printf("File open failed\n");
        exit(0);
    }
    //snprintf(line,strlen(line),"%s%s",uname,passwd);
    if(fputs(str,fp)==EOF) {
        printf("Error in writing to file\n");
    }
    return true;

    fclose(fp);

}