Thread: segmentaion fault with File Input/Output

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    40

    segmentaion fault with File Input/Output

    Hi,

    I have a text file with the following formating:
    +
    12
    12

    I am making a program that reads the file and takes the file name that the user entered and conctenate the .out to the new file and output the results for example 12+12=24 so 24 should go in the new file. I am getting the segmentaion fault when I try to scan the name of the file from the user.


    My code:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include <string.h>
    
    int main(int argc,char**argv){
    char c[10];  /* declare a char array */
      FILE *file;  /* declare a FILE pointer  */
      char *filename;
      printf("Please enter the file name: ");
      scanf("%s",&filename);
      char *cc;
      char *dd=".txt";
      cc=strcat(filename, dd);  // the file name
       file = fopen(cc, "r");
      /* open a text file for reading */
    
      if(file==NULL) {
        printf("Error: can't open file.\n");
        return 1;
      }
      else {
        printf("File opened successfully. Contents:\n\n");
    
        while(fgets(c, 10, file)!=NULL) {
          /* keep looping until NULL pointer... */
          printf("String: %s", c);
          /* print the file one line at a time  */
        }
    
        printf("\n\nNow closing file...\n");
        fclose(file);
        return 0;
      }
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use an array instead of a pointer. filename is just a pointer, which points to nothing specific. That is to say, it's uninitialized, so it just points to some arbitrary point in memory, which likely isn't yours to play with. Use an array, or allocate some memory for your file name.


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

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    thanks that worked I am now trying to add the 12 and the 12: It only reads the +11: its only reading the first characters.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include <string.h>
    
    int main(int argc,char**argv){
    char c[10];  /* declare a char array */
      FILE *file;  /* declare a FILE pointer  */
      char filename[20];
      printf("Please enter the file name: ");
      scanf("%s",&filename);
      char *cc;
      char *dd=".txt";
      cc=strcat(filename, dd);  // the file name
       file = fopen(cc, "r");
      /* open a text file for reading */
    
      if(file==NULL) {
        printf("Error: can't open file.\n");
        return 1;
      }
      else {
        printf("File opened successfully. Contents:\n\n");
        int one;
        int two;
        int three;
    
        while(fgets(c, 10, file)!=NULL) {
          one=c[0];
          two=c[1];
          three=c[2];
          printf("%s", &one);
    
        }
    
        printf("\n\nNow closing file...\n");
        fclose(file);
        return 0;
      }
    
    }
    Last edited by sara.stanley; 04-03-2006 at 10:41 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `main':
    foo.c:10: warning: char format, different type arg (arg 2)
    foo.c:11: warning: ISO C90 forbids mixed declarations and code
    foo.c:23: warning: ISO C90 forbids mixed declarations and code
    foo.c:31: warning: char format, different type arg (arg 2)
    foo.c: At top level:
    foo.c:5: warning: unused parameter 'argc'
    foo.c:5: warning: unused parameter 'argv'
    Fix your printf and scanf calls for starters.
    Also // is not a comment in standard C
    It is a comment as an extension to C89, or in C99 or C++

    > cc=strcat(filename, dd); // the file name
    > file = fopen(cc, "r");
    cc is useless, you may as well just do
    strcat(filename, dd);
    file = fopen(filename, "r");
    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.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    1) You don't have a read to get the operator.
    2) In this code:
    Code:
        while(fgets(c, 10, file)!=NULL) {
          one=c[0];
          two=c[1];
          three=c[2];
          printf("%s", &one);
    you can't expect to move the characters into ints and expect a number out of it. After you read a number line, use atoi() to convert the buffer into a number you can do math on:
    Code:
    fgets(c, 10, file);
    one=atoi(c);
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Or use scanf() to get ints, like they were created to do.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Or use scanf() to get ints, like it was originally created to do.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    thanks guys I got it to work..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Input/Output Files
    By zrsmith2 in forum C Programming
    Replies: 3
    Last Post: 06-15-2008, 10:30 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM