Thread: hi i am new plz help me

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    26

    Thumbs down hi i am new plz help me

    i attached the code below
    The i/p txt file is like this
    939,MUM,ord,396,2,3.0,4.0
    100,bom,nru,1009,10,4.0,6.0

    and i want to add 3.0+4.0=7.0
    i.e
    First i want to read the txt file add last two columns writing it into another txt I written the code and i run the code i am getting
    ------------------------------------
    939,MUM,ord,396,2,3.0,4.0
    100,bom,nru,1009,10,4.0,6.0

    Segmentation fault(coredump)
    ----------------------------------------------

    please help me in this

    Thanks & regards
    vijay
    Last edited by vijay85; 01-13-2009 at 11:29 AM. Reason: not properly formatted

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. Learn to indent code, eg.
    Code:
    #include<stdio.h>
    void main()
    {
        char temp[10];
        FILE *input1 = NULL;
        FILE *input2 = NULL;
        unsigned char buffer[100], lTempData[7][20];
        char *lPtr = NULL;
        int i =0;
        input1 = fopen("/u/n073313/vijay/File.txt", "rb");
    
        fread(buffer, 1,100, input1);
        {
            printf("First line %s\n",buffer);
            lPtr = strtok(buffer, ",");
    
            strncpy(lTempData[i], lPtr, strlen(lPtr));
    
            while (lPtr != NULL)
            {
                i++;
                strncpy(lTempData[i], lPtr, strlen(lPtr));
            }
    
            for (int j =0; j< 7 ; j++)
                printf("Values %s\n", lTempData[i]);
    
            int a = atol(lTempData[5]);
            int b = atol(lTempData[6]);
    
            int c = a+b;
    
            printf("Values %d %d result %d\n", a, b, c);
            sprintf(temp, "%d", c);
            input2 = fopen("File2.txt", "r");
            fwrite(c, 1, 10, input2);
            fclose(input1);
            fclose(input2);
    
        }
    
    }
    2. main returns int, not void.

    3. using fread to read a text file is very poor.
    a) it won't stop at a newline
    b) it won't have a \0, for the benefit of the following string functions
    c) it should have been in a loop, if you want to process the whole file.
    Code:
    while ( fgets( buffer, sizeof buffer, input1 ) != NULL ) {
      // do stuff with each line
    }
    4. strncpy(lTempData[i], lPtr, strlen(lPtr));
    It's nice that you're using strncpy to protect your strings from overflow, but
    a) you should use the size of the destination, not the size of the source
    b) you also need to ensure a \0 is copied (strncpy doesn't always do this).

    5. while (lPtr != NULL)
    What changed lPtr in this loop?
    If it gets into the loop, it's not getting out anytime soon.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    thanks for reply salem.
    Code:
    :
    #include<stdio.h>
    void main()
    {
    char temp[10];
    FILE *input1 = NULL;
    FILE *input2 = NULL;
    unsigned char buffer[100], lTempData[7][20];
    char *lPtr = NULL;
    int i =0;
    input1 = fopen("/u/n073313/vijay/File.txt", "rb");
    
    fread(buffer, 1,100, input1);
    {
    printf("First line %s\n",buffer);
    lPtr = strtok(buffer, ",");
    
    strncpy(lTempData[i], lPtr, strlen(lPtr));
    
    while ( fgets( buffer, sizeof buffer, input1 ) != NULL )
    {
    i++;
    strncpy(lTempData[i], lPtr, strlen(lPtr));
    }
    
    for (int j =0; j< 7 ; j++)
    printf("Values %s\n", lTempData[i]);
    
    int a = atol(lTempData[5]);
    int b = atol(lTempData[6]);
    
    int c = a+b;
    
    printf("Values %d %d result %d\n", a, b, c);
    sprintf(temp, "%d", c);
    input2 = fopen("File2.txt", "r");
    fwrite(c, 1, 10, input2);
    fclose(input1);
    fclose(input2);
    
    }
    
    }
    i changed as u mentioned but i am getting the o/p as
    ----------------------------------
    cc -o vijay vijay.c
    "vijay.c", line 15.6: 1506-068 (W) Operation between types "char*" and "int" is not allowed.
    "vijay.c", line 36.8: 1506-280 (W) Function argument assignment between types "const void*" and "int" is not allowed.
    $ ./vijay
    First line 939,MUM,ord,396,2,3.0,4.0
    100,bom,nru,1009,10,4.0,6.0

    Values 939
    Values 939
    Values 939
    Values 939
    Values 939
    Values 939
    Values 939
    Values 0 0 result 0
    Segmentation fault(coredump)
    -------------------------------------------------------------
    please help me in this
    thanks & regards
    vijay

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) You did not make the changes Salem requested. You have not indented the code and main still return void, not int as it should be!
    2) If you don't mind, point out on what lines the error occurs!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Line 15:
    Code:
    lPtr = strtok(buffer, ",");
    Perhaps you are missing the prototype for strtok?


    Line 36 is this:
    Code:
    	fwrite(c, 1, 10, input2);
    Since 'c' is an integer, not the address of the data you want to write, I'm not at all surprised. Also, you are trying to write 10 items of 1 byte each - an integer is not 10 bytes in any architecture I know of.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    hi i changed from void main() to int main().
    even though i am experiencing the same problem.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    fread(buffer, 1,100, input1);
    {
    printf("First line %s\n",buffer);
    lPtr = strtok(buffer, ",");
    Who said that after fread the buffer is nul-terminated?
    and you are using functions that will work correctly only on the nul-terminated strings
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    Code:
    #include<stdio.h>
    int main()
    {
    char temp[10];
    FILE *input1 = NULL;
    FILE *input2 = input2 = fopen("File2.txt", "w");
    unsigned char buffer[100], lTempData[7][20];
    char *lPtr = NULL;
    int i =0;
    input1 = fopen("/u/n073313/vijay/File.txt", "rb");
    
    fread(buffer, 1,100, input1);
    {
    printf("First line \n%s\n",buffer);
    lPtr = strtok(buffer, ",");
    
    strncpy(lTempData[i], lPtr, strlen(lPtr));
    
    while ( fgets( buffer, sizeof buffer, input1 ) != NULL )
    {
    i++;
    strncpy(lTempData[i], lPtr, strlen(lPtr));
    }
    
    for (int j =0; j< 7 ; j++)
    printf("Values %s\n", lTempData[i]);
    
    int a = atol(lTempData[5]);
    int b = atol(lTempData[6]);
    
    int c = a+b;
    
    printf("Values %d %d result %d\n", a, b, c);
    sprintf(temp, "%d", c);
    input2 = fopen("File2.txt", "r");
    fwrite(c, 1, 10, input2);
    fclose(input1);
    fclose(input2);
    
    }
    
    }
    o/p i am getting is

    $ cc -o vijay vijay.c
    "vijay.c", line 15.6: 1506-068 (W) Operation between types "char*" and "int" is not allowed.
    "vijay.c", line 36.8: 1506-280 (W) Function argument assignment between types "const void*" and "int" is not allowed.
    $ ./vijay
    First line
    939,MUM,ord,396,2,3.0,4.0
    100,bom,nru,1009,10,4.0,6.0

    Values 939
    Values 939
    Values 939
    Values 939
    Values 939
    Values 939
    Values 939
    Values 0 0 result 0

    the code above is not adding 3.0+4.0=7.0 & 4.0+6.0=10.0

    please help in this
    The requirement i have is first opening a .txt file reading the .txt file adding last two colums and writing the output in another .txt file.
    Hope i am clear with my requirement.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so you have change void main to int main, which is correct change, but it's not one of the more important ones - it certainly won't fix the errors/warnings you are seeing. I suggested what those are caused by.

    You are reading using fgets, but:
    Code:
    fread(buffer, 1,100, input1);
    what is this line doing?


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 09:54 AM
  3. plz help me with simple string comparison.
    By MegaManZZ in forum C++ Programming
    Replies: 11
    Last Post: 02-18-2008, 01:11 PM
  4. Anyone plz help me
    By Rose_Flowers in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2003, 12:01 PM
  5. help plz plz
    By nsssn73 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 08:44 AM