LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2009, 11:46 AM   #1
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 899

Rep: Reputation: 70
Question Storing a filename entered from the keyboard.


Hi guys,

I must have an attack of brainfade. This problem's got me beat. This code below when compiled just generates a segmentation fault. I've commented it so you can see what I'm trying to do. Where am I going wrong? Thanks:

Code:
 
#include <stdio.h>

int main(void)
{
    FILE *fp;
    int c;
    int b=0;
    char s[25];          //declare array 's' to hold the filename

printf("Enter local filename: ");
fgets(s,20,stdin);                  //read string from keyboard into   
                                    //array 's'


    fp = fopen(s , "r"); // no errorcheck    //open the file referrenced by 's' in read-mode.

    while((c = fgetc(fp)) != EOF)
          {
          b++;         //count number of characters in the file
          }

    fclose(fp);

printf("The number of characters in %s is:%d " ,s,b); //print out  
                                                      //total characters

    return 0;

}
 
Old 09-17-2009, 12:06 PM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
I first thought that you need a \0 to terminate the string but
http://www.cplusplus.com/reference/c.../cstdio/fgets/

states that it is appended. But what sruged me in fgets is that the newline char is also put into the string s.
But this all is just guessing and a quick shot. Maybe debugging with

http://www.ibm.com/developerworks/library/l-gdb/

will help

cheers Zhjim
 
Old 09-17-2009, 12:49 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Don't have a solution (yet), but I can point you to the actual error:

The file cannot be found (even though it is there).

I change the code a bit to get that answer:
Code:
#include <stdio.h>

int main(void)
{
  FILE *fp;
  int c;
  int b=0;
  char s[25];  

  printf("Enter local filename: ");
  fgets(s,20,stdin);                  

  fp = fopen(s,"r"); 

  if(fp==NULL) {
    printf("Error: can't open file.\n");
    return 1;
  }
  else {
    while((c = fgetc(fp)) != EOF)
    {
      b++;         
    }
    fclose(fp);
    printf("The number of characters in %s is:%d " ,s,b); 
    return 0;
  }
}
Output of a test run:
Quote:
$./blaat
Enter local filename: one.txt
Error: can't open file.
$ ls one.txt
one.txt
A clean, although unexpected, run. How is the file closed when an error occurs? (don't have the c knowledge to answer that myself)

An strace shows the following (relevant part, without the above addition):
Quote:
write(1, "Enter local filename: ", 22Enter local filename: ) = 22
read(0, one.txt
"one.txt\n", 1024) = 8
brk(0) = 0x804a000
brk(0x806b000) = 0x806b000
open("one.txt
", O_RDONLY) = -1 ENOENT (No such file or directory)
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV +++
Process 24775 detached

Last edited by druuna; 09-17-2009 at 12:52 PM. Reason: Made C code more readable
 
Old 09-17-2009, 12:57 PM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Your first problem is that you don't check the result of fopen. If the file does not exist you continue with fp equal to NULL.

And fgets keeps the newline character in, so s contains a '\n' and therefore fopen WILL return NULL.

Basic debugging with some printfs and a perror would have revealed it.

Last edited by Wim Sturkenboom; 09-17-2009 at 01:00 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Change name of backup file in ext3 from filename~ to .filename~ Libu Linux - General 2 07-21-2008 09:29 PM
Storing part of a filename as a variable VTGuitarMan Programming 5 03-12-2008 06:47 AM
A newb has entered. Hikage001 LinuxQuestions.org Member Intro 14 09-08-2007 03:47 PM
Convert static library (Filename.a) to dynamic shared object (filename.so) afx2029 Linux - Software 4 08-17-2007 06:07 AM
change uploaded files from filename.avi to filename.avi.html like www.rapidshare.de latheesan Linux - Newbie 3 06-16-2005 04:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration