Quick Question.

This is a discussion on Quick Question. within the C Programming forums, part of the General Programming Boards category; [code] #include <stdio.h> #include <string.h> main() { int tall,wide,size,x=0; char line[100],temp[100]; FILE *ifp; printf("\n\n Opening OBJECT.DAT "); if ((fopen("object.dat", "r")) ...

  1. #1
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77

    Quick Question.

    [code]
    #include <stdio.h>
    #include <string.h>

    main()
    {
    int tall,wide,size,x=0;
    char line[100],temp[100];
    FILE *ifp;


    printf("\n\n Opening OBJECT.DAT ");

    if ((fopen("object.dat", "r")) == NULL)
    {
    printf("\n error opening file object.dat ");
    }


    size=strlen(line);
    printf("\n %s",line);


    }
    [\code]

    I'm using this code to open up "object.dat"...then i want to read in the firstline of this file...im going to count backwards through the characters...and stick the first set in one variable (temp), then set the int tall=atoi(temp)...the object.dat is going to look like:

    "400 200"

    and i have to read both of those memories into 2 int variables...Can anyone add a proper fgets line to my above code to make it read the line in properly..i couldnt get it to work...i also need to stick a null pointer wherever the space occurs..can anyone help real fast? Thanks.

  2. #2
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    Well, I got my fgets() working...but how do I recognize when I come across a space in my line? What is the space character...when I come across a space, I want to stop...

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    i think using sscanf instead might help.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    If you want to stop at the first space you can either just use fscanf or combine fgets and sscanf:
    Code:
    fscanf ( line, sizeof line, fp );
    sscanf ( line, "%s", temp );
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-27-2005, 12:05 AM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 04:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 05:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 12:04 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 06:46 PM

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