• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

How do you read in a certain number of lines from a file?

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  This is how I was trying to do it but was getting a seg fault. Could someone help me fix it?
   
 
Rancher
Posts: 259
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hanna

With %d in a printf() you need an integer value. But with %d in (s)scanf you need the address of an integer variable where a value can be stored. At the moment you have:
This means that the addresses of where the input %d values are going to be stored are the values of e.fromVertex and e.toVertex; you need to prefix them with & to obtain their addresses.

Basically the code is trying to store the input values in places where it shouldn't, hence the segfault.
 
John Matthews
Rancher
Posts: 259
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although you've also got:
and:
which means you will probably get a segfault if your input file has lines longer than 100 chars.

Presumably the 100 should be a 1000, and you should also change the fgets() to:
so when you change the size of buffer you only need to do it in one place. Similarly in the other fgets()s. (You don't need num2 - just re-use num.)
 
Hanna Roberts
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you I forgot I needed to use the & for integers. That fixed the seg fault
 
John Matthews
Rancher
Posts: 259
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your compiler should have told you; gcc detects printf/scanf bugs like this.
 
Hanna Roberts
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I were to look up settings/tutorials for my complier what would I search? because my complier doesn't give those error messages for me.
 
John Matthews
Rancher
Posts: 259
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried googling for your compiler? Which one are you using?
 
Marshal
Posts: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are on a Unix‑like system (including MacOS and Linux) you shouldd know which compiler you are using because you have to use a command like gcc -o XYZ xyz.c. In his book Modern C (Manning 2020) (page 7), Jens Gusted says Windows® support isn't quite as good and he cites this blog by Chris Wellons.
 
lowercase baba
Posts: 12935
65
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If you are on a Unix‑like system (including MacOS and Linux) you should know which compiler you are using because you have to use a command like gcc -o XYZ xyz.c.


yeah, you'd think that. I can't tell you how many places i've worked that alias stuff..."more" gets aliased to "less".  "grep" gets aliased to "egrep" or whatever...so just because you typed "gcc", that doesn't mean you are running gcc.
 
Hanna Roberts
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could it be because I was running it on an IDE ? the one I use is called CLion
 
John Matthews
Rancher
Posts: 259
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not familiar with CLion, but as I suggested, google is (probably) your friend; searching for "clion compiler options" brings up this:
https://intellij-support.jetbrains.com/hc/en-us/community/posts/360000004970-Add-Compiler-Flags

Any use? Try adding the -Wall option if it is.
 
Hanna Roberts
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try looking, thanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic