reading in a file and storing it in a string.

closed account (Dy7SLyTq)
So, before I go any further, let me explain... I know how to do this in c++ with fstream and std::string and getline and so on and so forth. Im writing my code solely in c however. I can't get g++ installed so figured it was a good excuse to learn c instead of using the equivalent c++ abstracts.

My problem is, I'm making a game in c that I have made in c++ but have ran into an issue with my map. I want to read in my map from a file which just looks like this:
Name of Town
* * * * * * *
* * * * * * *
* * * * * * *
etc...

so i tried using fscanf to first read in the name of the town (stored in a char*) then read in the characters (in this case '*')(not including white spaces becuase i can just print those) into another char*. what is the better way to do this?
You could use fgets() and then sscanf().

Or use fscanf() to get everything you need.

Remember to read in string, you use %s and char is %c and make sure you include the reading of white space

1
2
3
4
5
while(!feof(somefile))
  {
      //code here
      //
  }
(Sorry, this isn't a directly helpful answer)
If you're on Windows you can try installing MinGW and then using clang for C++11 stuff ;)
closed account (Dy7SLyTq)
no im on linux. idk why though. ive installed g++ before but it just wont work for some reason
try running

sudo apt-get install g++

or

yum install gcc-c++
closed account (Dy7SLyTq)
the first one i have tried multiple versions multiple times. whats yum?
It does the same thing as apt-get

Before I go on, what linux distro are you running?
while(!feof(somefile)) in C is just as wrong as while(!somefile.eof()) in C++. Where do these things even come from?
*checks this website* oh.. *files a bug report*
closed account (Dy7SLyTq)
ubuntu. ive actually done it before on the same hard drive with the same disc on the same network. idk why it doesnt work now. ill try yum. can someone tell me what the difference between nano and vim?
closed account (Dy7SLyTq)
@smac89: I installed yum and then tried yum install gcc-c++. it said i need root access. i tried it with sudo and said it couldnt find the package
can someone tell me what the difference between nano and vim


vim ( http://www.vim.org ) is modernized vi, which is the Unix text editor from the 70s. It's very powerful, and very hard to learn. Some people (myself included) use it for programming.

nano ( http://www.nano-editor.org ) is a simple text editor that was a slightly improved pico, which was the text editor created for the popular Unix mail client pine. It's easy to learn, but it's text-oriented, not program-oriented.
closed account (Dy7SLyTq)
yeah i figured that. i was told to use that when i was setting up lamp. why is vim hard? i use it and it took me about ten minutes to learn, and i had no prior knowledge of how to use it
Ok that's weird because I am also running Ubuntu and this actually came installed with the distro. Try this:

sudo apt-get update

sudo apt-get install build-essential

If you type 'man g++' and you don't get a manual page, then try this link

sudo apt-get install gcc-4.7 g++-4.7
Last edited on
closed account (Dy7SLyTq)
yeah i know its weird. this is the first time i havent been able to get it. but thanks for the help im trying it right now
closed account (Dy7SLyTq)
dude you rock that worked. i can compile c++ now. ty
Glad to help

@Cubbi, what is a good way of doing that then while still doing it in c?
Last edited on
Same as in C++: perform the input, then test if it failed:
1
2
int c; // note the type
while ((c = fgetc(fp)) != EOF) {

or
1
2
int x,y;
while (fscanf(fp, "%d %d", &x, &y)==2) { 

or
while(fgets(buffer, sizeof buffer, fp) != NULL) {
Topic archived. No new replies allowed.