Sorting integers and characters in C

This is a discussion on Sorting integers and characters in C within the C Programming forums, part of the General Programming Boards category; I've been having trouble creating a sorting program in C. I can get input from a text file, output that ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    Sorting integers and characters in C

    I've been having trouble creating a sorting program in C. I can get input from a text file, output that information into an Array and sort it numerically, though I've been unable to take into account characters from a text file.

    What I'm looking to do is check the text file for characters, if a character is found, to send this to a seperate array, I've been messing around with the isalpha() function, when I run the below source code, the program seems to think every character and integer is an integer.

    Code:
    int Seperate_Alph_Int()
    {
    	FILE *fp;
    	fp = fopen("c:\\test.txt", "r");
    	while (fscanf(fp, "") != EOF) 
    	{
    		if(isalpha(fscanf(fp, "")) != 0) {
    			printf("Character");
    			return;
    		}
    		if(isalpha(fscanf(fp, "")) == 0) {
    			printf("Integer");
    			return;
    		}
    	}
    }
    Is there a way to get this to work? If not is there a better way of doing this?

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by kev2310
    I've been having trouble creating a sorting program in C.
    Is this C or C++? You posted in the C++ programming forum. If it is really C, I will move this thread.

    Quote Originally Posted by kev2310
    I can get input from a text file, output that information into an Array and sort it numerically, though I've been unable to take into account characters from a text file.
    Why don't you sort an array of strings instead?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    Sorry, I thought I was posting in the C board, this is a C question and not a C++ question

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    fscanf(fp, "")
    I'm not sure what you want that to do. fscanf doesn't return a single character; if that's what you want, then you want to use fgetc instead.

    Do you have two different types of data in your file? Are they related to each other (i.e., name and number for a single person together)? If so, you'd want to keep those related pieces of information together.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 07:38 PM
  2. Convert array of characters in file to integers
    By millsy5 in forum C Programming
    Replies: 4
    Last Post: 10-02-2009, 11:41 AM
  3. Replies: 15
    Last Post: 11-12-2008, 05:12 PM
  4. Convert string of characters to integers
    By Digital_20 in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2008, 09:40 PM
  5. send() characters from integers
    By joecaveman in forum C Programming
    Replies: 2
    Last Post: 05-17-2005, 07:38 PM

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