Thread: Need help porting a C++ program to C

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    3

    Need help porting a C++ program to C

    Hello,

    I am learning C and would like to port a short C++ program. I'm sure that the code is full of awkward implementations but getting this to work will go a long way. The original source was written by Ben Humphrey of gametutorials.com. Below is the line I don't understand how to approach. How would I go about writing a C equivalent of this?

    Code:
    fin >> strTemp >> room.strRoomNorth;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You would need to post the types of fin, strTemp, and room.strRoomNorth. We could guess that fin is a fstream, and perhaps strTemp and strRoomNorth are some kind of strings, but what exactly they are would determine what exactly you would write in C.
    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
    Oct 2018
    Posts
    3
    fin is ifstream, strTemp is string, room is a structure and strRoomNorth is a string.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah. fin >> strTemp reads non-whitespace characters into strTemp from the file input stream represented by fin. If you didn't care about the fact that the number of non-whitespace characters read is not fixed, you could use scanf with the %s format specifier and some field width (i.e., corresponding to whatever you choose to declare as the size of strTemp in C). If you do care, then you might need to do something like implement a loop with say, fgetc to read until the first whitespace character into a null terminated string stored in a dynamic array that you manually keep track of the size and capacity such that you can expand it as needed.

    EDIT:
    By the way, why are you porting a C++ program to C as part of your learning process in C? You don't seem to know C++, so it seems like a rather strange way to go about learning C as you would need to learn C++ too, and trying to learn C and C++ together is a recipe for confusion.
    Last edited by laserlight; 10-08-2018 at 02:27 AM.
    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

  5. #5
    Registered User
    Join Date
    Oct 2018
    Posts
    3
    Thank you for your reply, I will explore those options. I want to write the same kind of program in C only and this source code is useful because it pretty much does exactly what I need.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 08-01-2009, 11:07 AM
  2. Porting
    By lrusso in forum Tech Board
    Replies: 4
    Last Post: 08-13-2004, 11:30 PM
  3. Porting from g++ to VC6
    By Hubas in forum Windows Programming
    Replies: 4
    Last Post: 03-22-2003, 09:30 PM
  4. Porting app from c to c++
    By GUI_XP in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2002, 03:31 PM
  5. porting c program from unix to windows
    By sunsail in forum Windows Programming
    Replies: 1
    Last Post: 10-19-2002, 06:55 AM

Tags for this Thread