Thread: Sorting integers and characters in C

  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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,336
    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, 08: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, 06: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