Thread: noob to C need a little help!!!

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    athens
    Posts
    21

    Exclamation noob to C need a little help!!!

    hi.I am a gentoo user.I want to create a little tool that will give to me stats of my computer.
    On gentoo command:
    Code:
    cat /proc/cpuinfo
    give me this:
    Code:
    processor       : 0
    vendor_id       : GenuineIntel
    cpu family      : 6
    model           : 15
    model name      : Intel(R) Core(TM)2 Quad CPU    Q6600  @ 2.40GHz
    stepping        : 11
    cpu MHz         : 2394.000
    cache size      : 4096 KB
    physical id     : 0
    siblings        : 4
    core id         : 0
    cpu cores       : 4
    apicid          : 0
    initial apicid  : 0
    fdiv_bug        : no
    hlt_bug         : no
    f00f_bug        : no
    coma_bug        : no
    fpu             : yes
    fpu_exception   : yes
    cpuid level     : 10
    wp              : yes
    flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm lahf_lm tpr_shadow vnmi flexpriority
    bogomips        : 4800.03
    clflush size    : 64
    power management:
    
    processor       : 1
    vendor_id       : GenuineIntel
    cpu family      : 6
    model           : 15
    model name      : Intel(R) Core(TM)2 Quad CPU    Q6600  @ 2.40GHz
    stepping        : 11
    cpu MHz         : 2394.000
    cache size      : 4096 KB
    physical id     : 0
    siblings        : 4
    core id         : 2
    cpu cores       : 4
    apicid          : 2
    initial apicid  : 2
    fdiv_bug        : no
    hlt_bug         : no
    f00f_bug        : no
    coma_bug        : no
    fpu             : yes
    fpu_exception   : yes
    cpuid level     : 10
    wp              : yes
    flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm lahf_lm tpr_shadow vnmi flexpriority
    bogomips        : 4800.16
    clflush size    : 64
    power management:
    
    processor       : 2
    vendor_id       : GenuineIntel
    cpu family      : 6
    model           : 15
    model name      : Intel(R) Core(TM)2 Quad CPU    Q6600  @ 2.40GHz
    stepping        : 11
    cpu MHz         : 2394.000
    cache size      : 4096 KB
    physical id     : 0
    siblings        : 4
    core id         : 1
    cpu cores       : 4
    apicid          : 1
    initial apicid  : 1
    fdiv_bug        : no
    hlt_bug         : no
    f00f_bug        : no
    coma_bug        : no
    fpu             : yes
    fpu_exception   : yes
    cpuid level     : 10
    wp              : yes
    flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm lahf_lm tpr_shadow vnmi flexpriority
    bogomips        : 4800.15
    clflush size    : 64
    power management:
    
    processor       : 3
    vendor_id       : GenuineIntel
    cpu family      : 6
    model           : 15
    model name      : Intel(R) Core(TM)2 Quad CPU    Q6600  @ 2.40GHz
    stepping        : 11
    cpu MHz         : 2394.000
    cache size      : 4096 KB
    physical id     : 0
    siblings        : 4
    core id         : 3
    cpu cores       : 4
    apicid          : 3
    initial apicid  : 3
    fdiv_bug        : no
    hlt_bug         : no
    f00f_bug        : no
    coma_bug        : no
    fpu             : yes
    fpu_exception   : yes
    cpuid level     : 10
    wp              : yes
    flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm lahf_lm tpr_shadow vnmi flexpriority
    bogomips        : 4800.15
    clflush size    : 64
    power management:
    so i want to exec this command on C

    This is my try.

    Code:
    #include <stdio.h>
    main()
    {
    char a,1,2 ;
    printf ("Welcome to fsproc.Created by Ember George");
    printf ("\n");
    printf ("1) CPU");
    printf ("\n");
    printf ("2) RAM");
    printf ("\n");
    printf ("Please select proc number:");
    scanf  ("%c",&a);
    if (a==1)
    here i want to exec cat /proc/cpuinfo and see the result;
    else if (a==2)
    here i want to exec cat /proc/meminfo and see the result;
    }
    can someone help me to do that???

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Not to dissuade you from learning C, but have you considered writing a shell script instead?
    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
    May 2009
    Location
    athens
    Posts
    21
    i am doing it for 2 reason.First off all i am full in love with C so practice with small and easy projects is one way to learn C!!!!The second reason is that on my university they tell as to do something on C.So i want something usefull,easy but not a calc or hello world.I want something like that but with a lot of stats!!!!!but i have no idea how to print the result of this proccess.when i press "1" on my program i want to take the stats of my cpu that is the result of /proc/cpuinfo!!!with "2" the stats of ram,with "3" the stats of disks etc!!!!i think that will be nice at the end
    Last edited by sk8harddiefast; 02-27-2010 at 11:40 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, then what you need to do is to open the relevant file, read its contents, and write the contents to standard output, and close the file when you are done. Easy, but a little more involved than writing a shell script.

    You can modularise your program by writing a function that basically does the same thing as cat, then call it for each file as needed.
    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
    May 2009
    Location
    athens
    Posts
    21
    and how to do that???because i don't know C very well.only basics like scanf,printf,if else and i know fprintf & fscanf but i have a little syntax problem!!!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sk8harddiefast
    and how to do that???because i don't know C very well.only basics like scanf,printf,if else and i know fprintf & fscanf but i have a little syntax problem!!!
    One easy way is to read and write character by character: use fopen(), fgetc(), putchar(), and fclose().
    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

  7. #7
    Registered User
    Join Date
    May 2009
    Location
    athens
    Posts
    21
    ok.I will google it and make a try.If i can't do it i will write back

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or even popen(), to read the output of that command directly into your program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    May 2009
    Location
    athens
    Posts
    21
    i can not do it
    i tried everything but i can not
    please if you can help me
    also i make this little program only to open and read the file but is not reading it!!!!
    #include <stdio.h>
    main()
    {
    FILE *fp;
    {
    if ((fp = fopen("/proc/cpuinfo","r")) == NULL)
    return;
    }
    }
    Last edited by sk8harddiefast; 02-27-2010 at 03:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob needs help :)
    By Cursius in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2009, 04:19 PM
  2. Noob in need of help.
    By gec5741 in forum C++ Programming
    Replies: 18
    Last Post: 01-23-2009, 03:25 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  4. Noob Questions...
    By Firemagic in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2006, 03:57 PM
  5. noob needs help!!!!:(
    By tykenfitz in forum C Programming
    Replies: 1
    Last Post: 07-10-2005, 08:49 AM