Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   retrival of data from file (http://www.go4expert.com/forums/retrival-data-file-t8446/)

ice1 24Jan2008 16:34

retrival of data from file
 
i am suppose to retrive data from an ini file... I am able to retrive the entire file but not able to read and store specific contents from a file for further manipulation.....
Code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


char function1(FILE *fp,char *str);
char function2(char ,char *);

int main(int argc, char *argv[])
{
FILE *fp;
char *str,*str1;
char ch;
if(argc<4)
{
 printf("Inadequate number of inputs");
}
else
{
        while(((ch=fgetc(fp))!=EOF))
        {
                if(ch=='[')
                function1(fp,str);
                printf("hhhh");
                break;
        }
}

}

char function1(FILE *fp,char *str)
{
        int a,i;
        char ch;
        char arr[20];
        while(((ch=fgetc(fp))!=EOF))
        {
          // if(ch!=']'||ch!='[')
        if( strcmp(fp,str ) == 0 )
        {
        for(i=0;i<20;i++)
        {
        arr[i]=ch;
 printf("%c",arr[i]);
        }
        function2(arr,*str2);
        }
        else
        return;
        }
}

char functon2(char arr,char *str1)
{
        int b;
        if(arr!=']'||arr!='[')
        b=strcmp(arr,str1);
        if(b!=1)
        printf("%s",str1);

}



i ve prob in function1 and function2..
help me out...

Salem 24Jan2008 22:18

Re: retrival of data from file
 
Step 1, work on the indentation. Consistent and clear indentation will save you from an awful lot of mistakes, and make it much easier for anyone reading your code.

Eg.
Code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char function1(FILE * fp, char *str);
char function2(char, char *);

int main(int argc, char *argv[])
{
    FILE *fp;
    char *str, *str1;                      /*!! these don't point anywhere */
    char ch;                                /*!! should be declared int, not char */

    if (argc < 4) {
        printf("Inadequate number of inputs");
    } else {
        while (((ch = fgetc(fp)) != EOF)) { /*!! you don't open the file */
            if (ch == '[')
                function1(fp, str);
            printf("hhhh");
            break;                          /*!! the loop exits after just ONE character */
        }
    }

}

char function1(FILE * fp, char *str)
{
    int a, i;
    char ch;
    char arr[20];

    while (((ch = fgetc(fp)) != EOF)) {
        /* if(ch!=']'||ch!='[') */
        if (strcmp(fp, str) == 0) {    /*!! see above, str isn't pointing anywere */
                                        /*!! in addition, fp is totally the wrong type */
            for (i = 0; i < 20; i++) {  /*!! why fill the array with the same char? */
                arr[i] = ch;
                printf("%c", arr[i]);
            }
            function2(arr, str);        /*!! was *str2, an undeclared variable */
        } else
            return;
    }
}

char function2(char arr, char *str1)    /*!! fix spelling of function name */
{                             
    int b;

    if (arr != ']' || arr != '[')
        b = strcmp(arr, str1);
    if (b != 1)
        printf("%s", str1);
}

All the !! comments are something you need to think about.

For what it's worth, I got these warnings, after fixing some things.
Code:

$ gcc -W -Wall -ansi -pedantic -O2 foo.c
foo.c: In function `main':
foo.c:11: warning: unused variable `str1'
foo.c: In function `function1':
foo.c:35: warning: passing arg 1 of `strcmp' from incompatible pointer type
foo.c:41: warning: passing arg 1 of `function2' makes integer from pointer without a cast
foo.c:43: warning: `return' with no value, in function returning non-void
foo.c:29: warning: unused variable `a'
foo.c: In function `function2':
foo.c:52: warning: passing arg 1 of `strcmp' makes pointer from integer without a cast
foo.c: At top level:
foo.c:8: warning: unused parameter 'argv'
foo.c: In function `function2':
foo.c:55: warning: control reaches end of non-void function
foo.c: In function `function1':
foo.c:45: warning: control reaches end of non-void function
foo.c: In function `main':
foo.c:25: warning: control reaches end of non-void function
foo.c:10: warning: 'fp' might be used uninitialized in this function
foo.c:11: warning: 'str' might be used uninitialized in this function

Yes it is hard work to make your code compile clean (compared to what some other compilers' will let you get away with), but for the vast majority of the time, they tell you something you need to take care of. For example, using an uninitialised value would be very bad.

2. Use much better names than function1() and function2(). That tells me nothing about what is going on.

Personally, I would suggest you begin by reading each line of the file using fgets(), then use another function to look for say '[' in that line.


All times are GMT +5.5. The time now is 14:46.