Integers into array.

This is a discussion on Integers into array. within the C Programming forums, part of the General Programming Boards category; Hello, I'm trying to move the integers in a file to an array and order them. I'm almost positive that ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Integers into array.

    Hello, I'm trying to move the integers in a file to an array and order them. I'm almost positive that I can order them using a sorting method. However, I'm having trouble getting the integers from the file into the array. I'm calling a function, int function(char* file, int* array), and passing argv[1] for the file and the array name (let's use array for the name). Then I fp=fopen(file, "r").

    So, should I be using fscanf or getc to read in the integers from the file? The integers in the file are listed as such:
    1
    2
    5
    7
    etc...

    This is what I currently have for this section of code: (stop is initially 1 and i is initially 0)
    Code:
    do{
    		fscanf(fp, "%d", &num);  /* am I using fscanf correctly? */
    
    		if(num == EOF){
    			stop = 0;
    		}
    		else	a[i] = num;
    			i++;
    			read++;
    	}
    	while(stop);
    If possible please do not just give the corrections; lead me in the direction that I need to go so that I may learn what I'm doing wrong.

    Thank you for the help in advanced.

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by livestrng View Post
    I'm having trouble getting the integers from the file into the array.
    What kind of trouble? Does it compile? If not, what errors do you get?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,668
    Check the return value of 'fscanf' - it will tell you how many objects were read.



    ITSA
    Socket Library!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also num is unlikely to achieve the value EOF by reading (past) the end of the file - fscanf() will return EOF if it fails due to end-of-file conditions.

    Code:
    		else	a[i] = num;
    		i++;
    		read++;
    Indentation corrected. Is that what you actually intended?

    --
    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.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    It compiles, but when run:
    ./num5 num5.txt
    It yields a Segmentation Fault.

    And what do you mean by check fscanf? Should it not just read in the first line?

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Possibly you are not taking care of the '\n' character. Your file looks like this:
    1\n2\n\3\n\4\5\n\6EOF
    So you read 1 and then you try to read \n. Which is not what you want.
    You can just do
    Code:
    else if (num != '\n)
       a[i] = num;
       i++;
    }
    to ignore the '\n' characters.
    Generally, when reading from file think of the \n characters. It is a common mistake to ignore them.
    Another way is to do this:
    Code:
    char buffer[1024];
    int num, i;
    
    for (i=0; fgets(buffer,1024,fp) != null; ++i)
       a[i] = atoi(buffer);
    effectively doing everything in two lines. Just an alternative.
    fgets() gets everything in the line and returns null if error or EOF. It also gets the \n character. It stores them in the buffer as characters. Then atoi() makes the char buffer[] to an int. More precisely, atoi() reads the first number and ignores everything else, thus also the \n. So it should work fine.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    Ok, I understand about \n. So should my code read:
    Code:
    int num, i = 0, read = 0, stop = 1;
    	FILE* fp;
    	fp = fopen(file, "r");
    	
    	do{
    		fscanf(fp, "%d", &num);
    
    		if(num == EOF){
    			stop = 0;
    		}
    
    		else	if(num != '\n'){
    				a[i] = num;
    				i++;
    				read++;
    			}
    	}
    	while(stop);
    	
    	return(read);
    Or should I take out the if statement checking num == EOF?
    Also just to put it out there, compiled as is I still get a Segmentation Fault.

    Thanks.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C_ntua View Post
    Possibly you are not taking care of the '\n' character. Your file looks like this:
    1\n2\n\3\n\4\5\n\6EOF
    So you read 1 and then you try to read \n. Which is not what you want.
    You can just do
    Code:
    else if (num != '\n)
       a[i] = num;
       i++;
    }
    to ignore the '\n' characters.
    But "num" is (I hope) an integer, since it's being read with %d - so '\n' will only be equal to num if the value in the list is 10 (assuming standard control character encoding).

    --
    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.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No. On end-of-file, fscanf will NOT put EOF into the variable -- it will return EOF. So you need to keep the return value of fscanf.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    <newline>s will be skipped as part of the input whitespace characters so
    Code:
    if(num != '\n')
    will always be true.
    Code:
    while (stop);
    can be replaced by
    Code:
    while (fscanf(fp, "%d", &num) != EOF);
    and is the file argument to fopen() a string literal? Just my 2 cents

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    while (fscanf(fp, "&#37;d", &num) != EOF);
    better
    Code:
    while (fscanf(fp, "%d", &num) == 1)
    because if the string contains some characters that cannot be parsed as integer - fscanf will return 0 and not initialize num...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  2. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 06:14 PM
  3. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  4. putting an array of 10 integers in order
    By MisterSako in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2004, 06:27 PM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM

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