Problem with text file input

This is a discussion on Problem with text file input within the C Programming forums, part of the General Programming Boards category; Hi everyone, I posted a while back about reading in a sparse matrix but I'm having some real trouble with ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    10

    Problem with text file input

    Hi everyone,
    I posted a while back about reading in a sparse matrix but I'm having some real trouble with reading in text input.

    My plan in case the file was not standardised was to read in a line at a time, then split that line down into elements based on a set of delimiters (probably "," and "\n")

    My program keeps segfaulting and it's driving me mad - I think I know where but not why

    Code:
    int fileopen(char *argv)
    {
    FILE *file = fopen( argv, "r" ); //Open textfile with name given as an argument (argv) in read-only mode. fopen returns 0 on failure
    
    	if (file == 0)
    	{
    		fprintf(stderr,"Error in opening file: %s \n",argv); 
    		return(0);
    	}
    	else
    	{
    		int dimensionRow, dimensionCol;
    		int row, col, val; // Location and value to populate
    		int lineno = 0; // lineno in textfile;
    		char linebuffer[50];
    		char *element = NULL;
    		
    		while (fgets(linebuffer, sizeof(linebuffer), file) != NULL)
    		{
    			element = strtok(linebuffer, ",");
    			while (element != NULL )
    			{
    				printf("%s",element);
                                    element = strtok(NULL, ",");
    			}
    				
    		}
    All i want to do is is be able to print an element so i know the function is working as intended but it keeps crashing. My ultimate goal will be to have a list of delimiters which i can use like strtok(linebuffer,delims)

    When i run it through gdb it crashed on the print statement.
    linebuffer seems to be filled as expected but if i type
    "print element"
    i get : $12 = 0xffffffffffffe1c0 <Address 0xffffffffffffe1c0 out of bounds>
    then if i type "step" is segfaults. So I think I must not be using strtok correctly but I'm really stuck.
    I've literally spent my entire day playing around with file input/output functions and I've read countless guides and snippets of code and I'm sure it's something silly but I'm at my wits end.

    Any help would be appreciated. Thanks

    P.s. Currently the textfile i'm reading in looks like this as a test: 5,7,23,42,1,2,5,2

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Hmmm.. Can you please provide us with a sample of your .txt?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Works fine for me. My guess is the way you're passing argv. Perhaps you just used 'argv' from main. But by the time your fileopen() function sees it, it needs to be a char *.
    So try calling it like this: fileopen(argv[1]) - assuming your command-line argument has the file's name.

    On second thought that doesn't make sense. If you had trouble opening the file you'd have gotten the error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input of a Text File
    By Chemeng11 in forum C++ Programming
    Replies: 2
    Last Post: 05-01-2011, 10:13 AM
  2. input integer in a text file
    By Kaustubh in forum C Programming
    Replies: 3
    Last Post: 08-04-2010, 12:14 AM
  3. Input From a Text File - A Specific Problem.
    By Eddie K in forum C Programming
    Replies: 4
    Last Post: 03-09-2006, 02:50 PM
  4. User input into a text file
    By Starr in forum C++ Programming
    Replies: 8
    Last Post: 01-10-2006, 07:52 PM
  5. Input from non-text file
    By starkhorn in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2004, 01:18 PM

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