Thread: Need URGENT and EXTREMELY QUICK help.

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    18

    Question Need URGENT and EXTREMELY QUICK help.

    Got two issues.

    1) I got that in a text fie and I want to open it 1 by 1, while adding them to a variable. The variables are in a Structure, but when I try to print it, it either doesn't print it at all or just goes crazy and prints a lot of garbage. Any thoughts?

    Damon,Johnny,35,0.275,LF,1,0.495,100,18

    2) I need to know how long it took my program to compile, is there a function or something around that can help me with that??

    Thanks in advance,

    Crash...

    PS. If possible I need this answer ASAP, project due this Wednesday. Thanks again.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The way homework works on Cprogramming is that you show us that you've done something (such as the code), then ask SPECIFIC questions, and we will do our best to help you.

    As to how long it took to compile, unless you have a LARGE project, it's probably not measurable with a stopwatch. In linux/unix there is a time command, to measure time it takes to execute something, so you could do "time gcc myprog.c", and it will show you the overall time, the time spent on the process and the time spent by your process in the kernel (system time). You probably just care about the overall time, but you may want to clarify that with your teacher.

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

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <windows.h>
    
    struct fich
    {
    int num;
    char name[10];
    char last[10];
    };
    
    int leer_fichero();
    
    int main(void)
    {
    leer_fichero();
    getch();
    }
    
    int leer_fichero ()
    {
        int er_dev = 0,i;
    FILE *ficheroian;
    struct fich r;
    if ((ficheroian = fopen("yankees2.txt", "r")) == NULL)
    {
    printf ( "Error en apertura del fichero para lectura \n " );
    er_dev = 1;
    }
    else
    {
    fread (&r, sizeof(r), 1, ficheroian);
    while (! feof(ficheroian))
    {
    printf ("%s: %s: %d",r.last,r.name,r.num);
    fread (&r, sizeof(r),1,ficheroian);
    }
    fclose (ficheroian);
    }
    return er_dev;
    }

    I'm not American, therefore some stuff are in Spanish, if you need any translation, just hit me with a message. Thank you.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    fread() is not the right way to read your structure from a text-file. Perhaps using fgets() and sscanf() would work better.

    --
    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
    Apr 2008
    Posts
    18
    What does this error mean?

    37 C:\Documents and Settings\lecrash\Desktop\Fantasy Baseball\Untitled1.cpp cannot convert `fich*' to `char*' for argument `1' to `char* fgets(char*, int, FILE*)'

  6. #6
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by matsp View Post
    fread() is not the right way to read your structure from a text-file. Perhaps using fgets() and sscanf() would work better.

    --
    Mats
    Fread works fine to read structures, but as I understand, it isn't very portable.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by IceDane View Post
    Fread works fine to read structures, but as I understand, it isn't very portable.
    Sure, if the structures were written with fwrite - and usually that means the file is called something like xxx.dat, whilst the original poster is reading a file called xxx.txt - and there is a line of text quoted [I presume] from this file in the original post - it doesn't look like a binary file to me.

    @lecrash: I think you have the arguments to fgets() mixed up - the first one is the string, second is the size that the string can hold, the third is the file you want to read from.

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

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    I tried fgets (&r, sizeof(r), ficheroian); AND fgets (&r, 1, ficheroian);

    But it doesn't work either way. It's my first time trying something like this in C, because I know how to do it in C++, but it's so complicated I can't seem to put my finger on it. :-s Suggestions?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, you need a temporary string, such as:
    Code:
    char str[1000];
    fgets(str, sizeof(str), file);
    You will then have a string, which can be used with sscanf() to extract the actual components.

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

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    Sorry for the delay on the response, had to refresh myself, haven't slept since Sunday. Anyways, about that temporary string how would I use sscanf() to extract my information from my text? Would it be sscanf(file); ?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have you used scanf()? sscanf() is the same, except it takes the data from a string, whcih is the first argument, then a format string, then the arguments to be filled in with scanf.

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

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    I think I didn't explain myself as I should have. So here I go again. I've been doing a fantasy baseball simulator, and I need a roster that should be in a TEXT FILE and should be called in. Then I have to show, Last Name, Name, Age, Ave. etc. All of this has to be read and stored in a variable from my structure so I can call it later and still have it there. I can do everything but get the variables to store and print. In the example I used on my first post that Johnny Damon's stats and I was trying to print those, but only the first 3, but I couldn't so that why I came here. =D

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and I'm helping you read that from the input file - since you are reading from a text-file, you will need to read a line of text with fgets(), and then split it up. Or you could, if you so wish, use fscanf() to read directly from the file, with the added consequence that if your data file is even a slight bit wrong, the whole reading function could lock up and never finish.

    Whichever way you choose, you will need to know how to use the scanf() family of functions, and the only difference is if you pass a string or a file to the respective Xscanf() function [and whether you need a fgets() or not - but I don't think that's where you are stuck at the moment?].

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

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    I have never in my life used anything else but scanf and prinf. First time seeing all this, and since my programs needs it I've been learning along. So let me see if I get this. First off, I'll think I'll use the fscanf(), since I will input the information in the file, therefore I'll make sure the data won't be wrong. So, to use the fsancf what would I need? Just the file and then go fscanf and fprintf?

    Btw, haven't thanked you for all your help... Thank you matsp. =D

    PS I'm driving to work, though it's only like 5 minutes from my house. So I won't reply as fast as I've been doing.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, but using fscanf() is risky in the sense that if you try to read a number and there is a "non-number" in that place, it will hang fscanf() will not accept any further input [unless you also write code to cope with this problem].

    Using fgets() and sscanf() will be essentially the same effort, with the difference that if one record is "broken", you will only "loose" that record, and the program will continue nicely on to do the rest of it's work.

    It's obviously up to you.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Urgent Programming Problem
    By mchap1643 in forum C Programming
    Replies: 5
    Last Post: 10-16-2003, 08:54 AM