Input From a Text File - A Specific Problem.

This is a discussion on Input From a Text File - A Specific Problem. within the C Programming forums, part of the General Programming Boards category; Hi, this is my first post so I'm unsure what sort of information people require to help me, as such ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    London
    Posts
    7

    Input From a Text File - A Specific Problem.

    Hi, this is my first post so I'm unsure what sort of information people require to help me, as such I apologise in advance if I don't explain myself very well!

    The Problem: I'm required to write a program to read 50 numerical values in from a text file, which has been provided, and to use these values in doing some calculations further on in the program. These values should be read directly from the file, without any unnecessary user interaction. These values are x and y values, all either positive or negative decimal numbers. They are arranged in the table as such:

    x1 tab y1
    x2 tab y2
    ...
    xn tab yn

    My Problem:
    I haven't any idea of where to start, to be honest. My lectures in programming have been hard to follow, to say the least and I can't get my head around file input. What I want to do for the moment is to write a mini program to solve the problem and to print a few of the read values to show that the program is doing what it has been designed to do. Any help and moreover explanation would be very much appreciated.

    Thank you in advance.

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    here...

    your main function should be something like this:

    Code:
    void fileread(FILE *fp)
    {
     int c;
    
     while ((c = getc(fp)) != EOF)
     {
       printf("%d", c);
     }
    }
    if you need help with anything else, just ask. if this code doesnt work, feel free to yell at me, cuz i didnt test it before posting. though if you put it right, you should be fine... ive used this code before

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    and

    do you know what youre doing with file i/o? it's incredibly easy once you understand it... from what I gather, you want to print the integers from just any random text file?

  4. #4
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    I'd suggest something more like:
    Code:
    void echofile(FILE *fp)
    {
        int n1, n2;
        int count = 0;
        char inbuffer[80];
    
        while (fgets(inbuffer, sizeof(inbuffer), fp))
        {
            ++count;
            if (sscanf(inbuffer, "%d%d", &n1, &n2) == 2)
            {
                printf("Line %d:  %d %d\n", count, n1, n2);
            }
            else
            {
                printf("Line %d is empty or has fewer than 2 numbers on it\n", count);
            }
        }
    }
    The "%d%d" (note, no whitespace between the two format specifiers) should work because sscanf() skips whitespace while finding the beginning of the value for each %d, so the first %d is terminated by the tab, the second %d skips the tab and is terminated by the end-of-line.
    Last edited by filker0; 03-09-2006 at 02:46 PM. Reason: Missing a ")"
    Insert obnoxious but pithy remark here

  5. #5
    Registered User
    Join Date
    Mar 2006
    Location
    London
    Posts
    7
    Quote Originally Posted by xixpsychoxix
    do you know what youre doing with file i/o? it's incredibly easy once you understand it... from what I gather, you want to print the integers from just any random text file?
    Well, I know what I want to do but I don't know how to structure it at all. Iffy, to say the least.

    Basically - I need more explanation rather than just a code posted because it is likely not to mean much to me. Is there a method to solve this problem?
    Last edited by Eddie K; 03-09-2006 at 02:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  2. Problem with File Input & Pointers
    By jenna in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 10:34 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Confusing problem with file input
    By Vorok in forum C++ Programming
    Replies: 3
    Last Post: 01-05-2003, 02:49 AM

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