File Input

This is a discussion on File Input within the C Programming forums, part of the General Programming Boards category; Hi.. Could anyone tell me what I did wrong? I kept getting segmentation faults. #include <stdio.h> #include <stdlib.h> int main(int ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    1

    File Input

    Hi.. Could anyone tell me what I did wrong? I kept getting segmentation faults.

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

    int main(int argc, char **argv)
    {
    FILE *fp;
    int num;
    char *name;
    fp = fopen (argv[1],"r");
    while (!feof(fp)) {
    fscanf (fp, "%d\t%s", &num, name);
    printf ("num=%d name=%s\n", num, name);
    }
    fclose (fp);
    return 0;
    }

    ie. fp contains data such as:
    123 abc
    456 def, etc..

    And I want to put them into arrays.

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >>while (!feof(fp))
    this is wrong, read this

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    i thought you had to type **argv[] in the main function's parameters yours is missing the []. I could be wrong though

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by killjoyntk
    i thought you had to type **argv[] in the main function's parameters yours is missing the []. I could be wrong though
    You've got one to many *.

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

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    i thought about that after i posted that, I always used *argv[] .

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    102
    Originally posted by quzah
    You've got one to many *.

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

    Quzah.
    Ive used char **argv many times, why is it wrong??

    What is the difference between char *argv[] and char **argv?

    if you access an array like its a pointer then it does not really matter right?

    Code:
    int main(int argc, char **argv)
    {
       int i;
       if(argv < 2) {
          prinf("need at least more then one argument");
        return(0); }
       
        for(i=argc; i > 0; i--)
        {
         printf("%s", **(argv+i));
        }
         return(0);
    }
    Last edited by squid; 04-18-2003 at 11:43 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by squid Ive used char **argv many times, why is it wrong??[/code]
    I wasn't saying that was wrong.

    Read my post over again. See what I quoted? "char **argv[]" is wrong. Like I said, one too many * in that.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    102
    Oops I missed that. In that case you are right. OOPS

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21