How you get data from a file and put it in a variable?

This is a discussion on How you get data from a file and put it in a variable? within the C Programming forums, part of the General Programming Boards category; Heres the code: Code: FILE * fileP; main() { fileP = fopen("C:\textad\stats.dat", "r+") ... fseek(fileP, 0, SEEK_SET); char name[36] = ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    41

    Question How you get data from a file and put it in a variable?

    Heres the code:

    Code:
    FILE * fileP;
    
    main()
    {
        fileP = fopen("C:\\textad\\stats.dat", "r+")
        ...
        fseek(fileP, 0, SEEK_SET);    
        char name[36] = ???;
    The first line of 'stats.dat' has a name, I want that name in 'char name', how do I do that?
    THERE IS NO PLACE LIKE 127.0.0.1

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Code:
    fgets(name, sizeof (name), fileP);
    Though it looks like you're compiling as C++ since C doesn't support variable declarations except at the top of a block of code.
    p.s. What the alphabet would look like without q and r.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    41
    I got this error:

    Line 7, *:\***l\Dev-Cpp\***\main.c, invalid initializer :\
    THERE IS NO PLACE LIKE 127.0.0.1

  4. #4
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Then you are compiling as C, it's not legal code so you're sure to get a diagnostic when you try and compile. Try this instead:
    Code:
    FILE * fileP;
    
    main()
    {
        char name[36];
        fileP = fopen("C:\\textad\\stats.dat", "r+")
        ...
        fseek(fileP, 0, SEEK_SET);    
        fgets(name, sizeof (name), fileP);
    p.s. What the alphabet would look like without q and r.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. ordered linked list
    By redmondtab in forum C Programming
    Replies: 48
    Last Post: 10-22-2006, 06:09 AM
  4. Editing a data file
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 02-05-2005, 03:21 PM

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