samedi 25 avril 2015

Operator << overload for ofstream


I would appreciate if you could take a look and tell me what im doing wrong. I want to overload an operator for writing object values into a file. Here is what i came up with.

template<typename T>ofstream &operator<<(ofstream &f, const config<T> &X)  
{
    if(X.attribName1.size()!=0)
    {f<<X.attribName1;
for(int i=0;i<3;i++)
        {

            f<<X.attribute1[i];
}}

return f;

}

Implementation :

FILE1NAME="Config_file_no1";
ofstream file1(FILE1NAME+".ini");
    ofstream file2;

    config<double> first;
    first.setAttribName1(file1name1);
    first.setAtt1(val2);


    file1<<first;

The file gets created but its empty. Anyone has any suggestions ? Im thankful for any help.


What is the fastest way to check an internet connection using C or C++?


I'm trying to write a program in C/C++ to check the internet connection on a windows machine. Any help is appreciated. Thank you.


Segmentation fault while using array of char arrays


i have C program. It compiles and works fine, without errors, gives proper results, but when i run debugger, i get segmentation fault. I'm advised to solve this issue, but i don't know whats wrong.

This causes problems:

char names[20][20]; //is this correct?

for(i=0;i<20;i++)
{
    strcpy(names[i],base[i].name);
}

for(i=19;i>=0;i--)
{
    for(j=0;j<i;j++)
    {
        if(strcmp(names[j],names[j+1])>0)
        {
            strcpy(temp,names[j]);
            strcpy(names[j],names[j+1]);
            strcpy(names[j+1],temp);
        }
    }
}

Those elements in base[i].name are:

char name[20];

base = (person*) malloc(20 * sizeof(person));

Generally speaking, all of involved structures have length 20, so I don't know what I'm doing wrong. I guess I'm trying to access memory which I shouldn't, but where?


How to pack the C code with NDK and run it on Android phone?


My team and I are working on a project by coding in C. We finished coding part, and then we need to bring the project into Android phone.

I installed Android SDK, NDK, eclipse. I try to run our C project in eclipse and it works.

So for the next step, what should we do? How to pack the code by using NDK and test it??

Thank you so much


JPEG source-code and quantization mode change - C language


I'm assigned to do a project that consists in changing the quantization in the JPEG source-code, from the quantization tables to Lloyd-Max quantization. The problem is not knowing what to do (I know how to change the quantization), but where to find the code I'm suposed to change. If someone is familiar with the libjpeg-turbo, could you give me some advice on doing so?


fast way to get integers 0, 1, and 2 when given a random one from the set


So basically

int num = rand(2); //random number from 0-2
int otherNum, otherOtherNum;
otherNum = implement this
otherOtherNum = implement this

For example, if num is 2, otherNum and otherOtherNum must be set to 0 and 1 (or 1 and 0).

How would you implement this? What's a fast way? Assume you can't use branching or look up tables.


Creating multiple threads in C: write pid, tid and return integer


I must do this:

Write a program whose main thread creates 3 other threads. Each ot these threads (different from the main thread) shall write its pid and tid and terminate returning an integer number between 1 and 3 and different from the value returned by the other threads. The main thread shall print in the standard out its pid and the value returned by each of the other threads.

I think I'm doing it right, except that I don't understand how should I return the integer numbers between 1 and 3

I've the following code so far:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#define DEBUG 0

// int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg);

// int pthread_join(pthread_t, void **value_ptr);

void *printID(void *arg){

    int i;

    unsigned long tid;
    tid = *((unsigned long *) arg);           

    printf("Thread tid: %lu Thread pid: %d \n", tid, getpid());

}

int main(int argc, char *argv[]) {

    pthread_t id1, id2, id3;

    pthread_create(&id1, NULL, printID, &id1);

    pthread_create(&id2, NULL, printID, &id2);

    pthread_create(&id3, NULL, printID, &id3);

    pthread_join(id1, NULL);

    pthread_join(id2, NULL);

    pthread_join(id3, NULL);

    printf("Main thread pid: %d\n", getpid());

    sleep(1);

}