Thread: Store word of a file to a variable

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    10

    Store word of a file to a variable

    Hello!,
    I have written a function to store user input.After the input the other input is stored with a \n. So its like

    X1
    X2
    X3

    . Is it possible to store X1 to a variable. Would i go for a loop which would store all the characters of the word until it reaches \n? I want to do the same thing with 2 files separately. Thank you!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, you can call fgets in a loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    10
    The part that is confusing me is,how will i save the word to a char until the /n and then goto the next line.I keep getting confused over the getc and gets functions

  4. #4
    I'm a computer snoopfrogg's Avatar
    Join Date
    Feb 2019
    Posts
    29
    Quote Originally Posted by frequencynip View Post
    The part that is confusing me is,how will i save the word to a char until the /n and then goto the next line.I keep getting confused over the getc and gets functions
    If I'm not mistaken fgetc() reads the character currently indicated by the internal file position indicator of the particular file pointer (fp).

    fgets() will stop reading the when either (n-1) characters are read and the and the NULL character is appended to the string (after any newline character), the newline character is read, or the end-of-file is reached.

    Check out out this link to read more about fgets() and fgetc(): C Programming/Stream IO - Wikibooks, open books for an open world
    "One of the best programming skills you can have is knowing when to walk away for awhile."

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by frequencynip View Post
    The part that is confusing me is,how will i save the word to a char until the /n and then goto the next line.I keep getting confused over the getc and gets functions
    gets() is depreciated in the C99 C Standard, and has been removed from the C11 C Standard.

    fgets() should used instead. You will have to deal with the trailing '\n' char at the end of the string input.

    You need to study a good book on the C Programming Language for a thorough understanding of all the stdio.h functions. This site does have a list of books, but needs a serious update!

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    10
    We are told to code with ISO C90 in uni for our projects

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by frequencynip View Post
    We are told to code with ISO C90 in uni for our projects
    That's fine, but still avoid the gets() function! You can exceed the length of the array with gets()! There is NO bounds checking in gets()!!!

    fgets() has always been available in all standards, including the C89/C90 Standard.

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    10
    So,i can store the word to a variable with fgets and afterwards how will i go to the next line? Hm

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by frequencynip View Post
    So,i can store the word to a variable with fgets and afterwards how will i go to the next line? Hm
    The first call to fgets() with input one line from the file, up to the length of the array passed as the second argument to fgets(), or the newline, whichever come first. The next call to fgets() will get the next line, etc... usually in a while() or for() loop.

    You need to attempt to code this yourself, then post the code with questions if you have trouble. We don't program the solution for you.

  10. #10
    Registered User
    Join Date
    Apr 2019
    Posts
    10
    Yea got that.I am returning from a trip atm.I will try to code when i am back.Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  2. Replies: 7
    Last Post: 09-04-2011, 09:29 PM
  3. Replies: 5
    Last Post: 12-31-2010, 10:10 AM
  4. Read first line of a file and store in a variable
    By Saeid87 in forum C Programming
    Replies: 5
    Last Post: 06-19-2009, 11:27 AM
  5. make std::cin store more than 1 word in a string?
    By pilot1 in forum C++ Programming
    Replies: 10
    Last Post: 05-20-2004, 02:18 PM

Tags for this Thread