Thread: Print nth Column in Tab Separated Data File

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    4

    Print nth Column in Tab Separated Data File

    Hello,

    I'm trying to print out a column in a data file, this is the contents of the data file:

    Code:
    1    2    3    4
    5    6    7    8
    9    10   11   12
    This is the source code:

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void main()
    {
        /* Declare Local Variables */
        FILE *fptr;                // File pointer for reading data file
        int numTraces = 4;        // Number of traces in file to be analysed
        int numSamples = 7661;    // Number of samples in file to be analysed
        int i = 0;
        int trace;                // Trace number to analyse
        float sample;            // Sample being scanned into program
        char prompt;            // Prompt to hold program
    
    
    
    
        /* Input column to analyse */
        printf("Please enter trace number to analyse (1 - %d): ",numTraces);
        scanf("\n%d", &trace);
        
        /* Open program.dat file */
            fptr = fopen("data.txt", "r");        // For read access only
            if (fptr == NULL)
            {
                printf("Unable to find file: [data.txt]\n\n\r");    // Error if the program.dat file can't be found
    
    
                /* Wait for enter key to close */
                printf("Press Enter to close the program.");
                getchar();
                exit(1);
            }
    
    
            i = trace;        // Initialise i to 0 + trace number to analyse
    
    
            /* Scan Information from File */
            while (fscanf(fptr, "%f", &sample) != EOF)    // Loop until end of file marker, i.e. no more values to calculate
            {
                if (i % numTraces == 0)
                {
                    printf("%f\n\r", sample);
                }
                i++;
            }
    
    
            /* Clean up */
            fclose(fptr);
    
    
            /* Wait for enter key to close */
            printf("Press any key followed by Enter to close the program.");
            scanf("\n%c", &prompt);
            exit(1);
    
    
    }
    So the user enters a number between 1 and 4 and then it should print on each line the numbers from that column.

    It's sort of working... by this I mean when you ask for:

    • column 1 it prints column 4
    • column 2 it prints column 3
    • column 3 it prints column 2
    • column 4 it prints column 1

    It's like it's transposing it somehow? I'm sure it's something simple, prehaps to do with the if statement that wraps around the printf statement?

    I look forward to hearing from you.

    Kind Regards

    Edward

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by edwardholmes91 View Post
    I'm sure it's something simple, prehaps to do with the if statement that wraps around the printf statement?
    Why not inspect that part of the code and see?

    Code:
    while (fscanf(fptr, "%f", &sample) != EOF)    // Loop until end of file marker, i.e. no more values to calculate
    {
        printf("> i %% numTraces = %d\n", i % numTraces);
        if (i % numTraces == 0)
        {
            printf("%f\n\r", sample);
        }
        i++;
    }
    Code:
    Please enter trace number to analyse (1 - 4): 1
    > i % numTraces = 1
    > i % numTraces = 2
    > i % numTraces = 3
    > i % numTraces = 0
    4.000000
    > i % numTraces = 1
    > i % numTraces = 2
    > i % numTraces = 3
    > i % numTraces = 0
    8.000000
    > i % numTraces = 1
    > i % numTraces = 2
    > i % numTraces = 3
    > i % numTraces = 0
    12.000000
    Press any key followed by Enter to close the program.0
    Also, "main()" returns int, not void: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    4
    Thank you for your reply. I can see what's happening, but I'm still not sure how I can modify the code to give the result that I'm looking for.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    So get out a pen and paper and start figuring out the logic.

    When you have an idea, start a new project and test just that logic. When you get it working, go back to your real program and update it with the working logic. If you can't get it working, post the simplified test version and we can help you with that.

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    4
    The actual data has 13 columns including an index (first column).

    So knowing this, I was able to use a switch case statement to convert the column number that the user entered to give the correct result:

    Code:
    switch (trace)	{
    	case 1:	 trace = 12; break;
    	case 2:  trace = 11; break;
    	case 3:  trace = 10; break;
    	case 4:  trace = 9;	 break;
    	case 5:  trace = 8;  break;
    	case 6:  trace = 7;  break;
    	case 7:  trace = 6;  break;
    	case 8:  trace = 5;  break;
    	case 9:  trace = 4;  break;
    	case 10: trace = 3;  break;
    	case 11: trace = 2;  break;
    	case 12: trace = 1;  break;
    	default:
    		printf("Incorrect entry, program will now exit.");
    		exit(1);
    	}
    It might be crude but seems to work!

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    A rather wordy way to simply calculate:
    Code:
    trace = 13 - trace;

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Well, good that you got it working. Of course, scaling the columns up or down might be more trouble with that solution than with a more clever one.

    All you really had to do was initialize "i" to 1, and in the loop, compare "i" and "trace" for equality to determine whether to print. Then, when "i" was incremented, roll it back to 1 if the number of columns was exceeded.

  8. #8
    Registered User
    Join Date
    Mar 2016
    Posts
    4
    Quote Originally Posted by algorism View Post
    A rather wordy way to simply calculate:
    Code:
    trace = 13 - trace;
    Hello algorism,

    Thanks, that's exactly what I was trying to achieve! I used this code to replace that ugly big switch case statement:

    Code:
    trace = numTraces - trace;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in comma separated numbers from a data file
    By littlemslexii in forum C Programming
    Replies: 8
    Last Post: 04-22-2013, 09:41 AM
  2. Replies: 3
    Last Post: 03-13-2013, 07:10 PM
  3. How to add a column of data to a .txt file?
    By Blah937 in forum C++ Programming
    Replies: 8
    Last Post: 07-07-2011, 02:59 PM
  4. Column count from data file
    By spiit231 in forum C Programming
    Replies: 6
    Last Post: 02-27-2008, 12:59 PM
  5. Replies: 10
    Last Post: 07-13-2003, 01:48 PM