Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Java programmer having problems with C.
New Posts  All Forums:Forum Nav:

Java programmer having problems with C.

post #1 of 20
Thread Starter 
Hey guys,
I'm really used to programming in Java, but I'm currently taking a C class and my teacher isn't the best... I have a problem and I just can't figure it out. I'm not asking for direct code, just a bump in the right way...

I have to make a program that receives a text file, reads it and counts the specific amount of integers are in that line. Eventually I will have a count of how many 0's, 1's, 2's ... 9's are in the text file and I will just have to output that.

I plan on using three functions, main, a getLine function and a countNums function.

I'm using the getLine function out of The C Programming Language by Brian W. Kerninghan and Dennis M. Rtichie.

The getLine functions takes a char[] and a int as a parameter. is stores the line read in the char[] and the int is the limit of the char[].

I have no clue how to start the countNums function... Or even what to do in it.

Chears
Griever
(14 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-3570K ASUS P8Z77-V Deluxe MSI R7950 Twin Frozr 3GB Corsair Vengeance 16GB 1600Mhz 
Hard DriveCoolingOSMonitor
OCZ Vertex Plus 120GB Corsair h100 Windows 7 Professional Asus VS228H-P 21.5In 
KeyboardPowerCase
Razer Black Widow Ultimate Corsair AX850 NZXT Switch 810 White 
  hide details  
Reply
Griever
(14 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-3570K ASUS P8Z77-V Deluxe MSI R7950 Twin Frozr 3GB Corsair Vengeance 16GB 1600Mhz 
Hard DriveCoolingOSMonitor
OCZ Vertex Plus 120GB Corsair h100 Windows 7 Professional Asus VS228H-P 21.5In 
KeyboardPowerCase
Razer Black Widow Ultimate Corsair AX850 NZXT Switch 810 White 
  hide details  
Reply
post #2 of 20
Make a pointer to a file and extract each char and test them against the numbers. An array to count the frequency of the digits.
Code:
File *fp;  /* for file pointer*/

fp=fopen(name,"r");

fp points to first character in the file. Then test it.
Edited by Makyl - 10/3/12 at 2:50pm
post #3 of 20
Thread Starter 
We're not aloud to use FIle sadly, :/.

Which is what confused me, there is no code to actually support the import. Or none that he taught us atleast. we're just supposed to call the getLine function and it all goes in.

When I run the program I'm running it like....

./a.out < eahsfkjahskf.txt
Griever
(14 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-3570K ASUS P8Z77-V Deluxe MSI R7950 Twin Frozr 3GB Corsair Vengeance 16GB 1600Mhz 
Hard DriveCoolingOSMonitor
OCZ Vertex Plus 120GB Corsair h100 Windows 7 Professional Asus VS228H-P 21.5In 
KeyboardPowerCase
Razer Black Widow Ultimate Corsair AX850 NZXT Switch 810 White 
  hide details  
Reply
Griever
(14 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-3570K ASUS P8Z77-V Deluxe MSI R7950 Twin Frozr 3GB Corsair Vengeance 16GB 1600Mhz 
Hard DriveCoolingOSMonitor
OCZ Vertex Plus 120GB Corsair h100 Windows 7 Professional Asus VS228H-P 21.5In 
KeyboardPowerCase
Razer Black Widow Ultimate Corsair AX850 NZXT Switch 810 White 
  hide details  
Reply
post #4 of 20
getline is usually used to read from stream like ./a.out 1245667788 to give the input. I don't remember about the files. Let me search and get back to you.

Try using fgets. I think that would work. but you would have to specify the maximum number of character in the file.
Edited by Makyl - 10/3/12 at 3:07pm
post #5 of 20
Thread Starter 
Yep, the < akfkajshfkjas.txt substitutes the text from the keyboard after you run the program.

This is the code I have for my countNums function...
Code:
int countNums(char s[], int lim, int num)
{
   int c, i, count;
   int test = num;

   for (i = 0; i < lim -1 && (c = getchar()) != '\0\; i++)
          if (test == c)
               count++;

return count;
}

No luck, just returns a 0...
Edited by Saucee - 10/3/12 at 3:18pm
Griever
(14 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-3570K ASUS P8Z77-V Deluxe MSI R7950 Twin Frozr 3GB Corsair Vengeance 16GB 1600Mhz 
Hard DriveCoolingOSMonitor
OCZ Vertex Plus 120GB Corsair h100 Windows 7 Professional Asus VS228H-P 21.5In 
KeyboardPowerCase
Razer Black Widow Ultimate Corsair AX850 NZXT Switch 810 White 
  hide details  
Reply
Griever
(14 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-3570K ASUS P8Z77-V Deluxe MSI R7950 Twin Frozr 3GB Corsair Vengeance 16GB 1600Mhz 
Hard DriveCoolingOSMonitor
OCZ Vertex Plus 120GB Corsair h100 Windows 7 Professional Asus VS228H-P 21.5In 
KeyboardPowerCase
Razer Black Widow Ultimate Corsair AX850 NZXT Switch 810 White 
  hide details  
Reply
post #6 of 20
I can't find anything. As far as I know you will have to use File to open a file to read from it. Maybe who knows some more will know how.

Sorry I could not be of more help.
post #7 of 20
Thread Starter 
Quote:
Originally Posted by Makyl View Post

I can't find anything. As far as I know you will have to use File to open a file to read from it. Maybe who knows some more will know how.
Sorry I could not be of more help.

Hey, no problem. Thanks for the help anyway...

The problem isn't inputing the file, I have that down. The problem is creating the counNum method

Oops, that's the java in me :x.
Griever
(14 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-3570K ASUS P8Z77-V Deluxe MSI R7950 Twin Frozr 3GB Corsair Vengeance 16GB 1600Mhz 
Hard DriveCoolingOSMonitor
OCZ Vertex Plus 120GB Corsair h100 Windows 7 Professional Asus VS228H-P 21.5In 
KeyboardPowerCase
Razer Black Widow Ultimate Corsair AX850 NZXT Switch 810 White 
  hide details  
Reply
Griever
(14 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-3570K ASUS P8Z77-V Deluxe MSI R7950 Twin Frozr 3GB Corsair Vengeance 16GB 1600Mhz 
Hard DriveCoolingOSMonitor
OCZ Vertex Plus 120GB Corsair h100 Windows 7 Professional Asus VS228H-P 21.5In 
KeyboardPowerCase
Razer Black Widow Ultimate Corsair AX850 NZXT Switch 810 White 
  hide details  
Reply
post #8 of 20
Thread Starter 
This is my main....
Code:

int main()
{
  char s[80];

  krgetLine(s, 80);
  printf(s);
  int test = countNums(s, 80, 1);
  printf("%f \n", test);
}

And this is my countNums function....
Code:

int countNums(char s[], int lim, int num)
{
  int i;
  int count = 0;
  for (i = 0; s[i] != '\0' || i < lim -1; i++)
    {
      if (num == s[i])
        count++;
    }
  return count;
}


And when I run it with a file containing a 1, I keep getting 0.... And I can't figure out why.
Edited by Saucee - 10/3/12 at 5:49pm
Griever
(14 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-3570K ASUS P8Z77-V Deluxe MSI R7950 Twin Frozr 3GB Corsair Vengeance 16GB 1600Mhz 
Hard DriveCoolingOSMonitor
OCZ Vertex Plus 120GB Corsair h100 Windows 7 Professional Asus VS228H-P 21.5In 
KeyboardPowerCase
Razer Black Widow Ultimate Corsair AX850 NZXT Switch 810 White 
  hide details  
Reply
Griever
(14 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-3570K ASUS P8Z77-V Deluxe MSI R7950 Twin Frozr 3GB Corsair Vengeance 16GB 1600Mhz 
Hard DriveCoolingOSMonitor
OCZ Vertex Plus 120GB Corsair h100 Windows 7 Professional Asus VS228H-P 21.5In 
KeyboardPowerCase
Razer Black Widow Ultimate Corsair AX850 NZXT Switch 810 White 
  hide details  
Reply
post #9 of 20
Code:
int countNums(char s[], int lim, int num)
{
  int i;
  int count = 0;
  for (i = 0; s[i] != '\0' || i < lim -1; i++)
    {
      k=atoi(&s[i]);
      if (num == k)
        count++;
    }
  return count;
}

Try this and see if it works.
post #10 of 20
Can you post your entire code, that should help a massive amount! smile.gif
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Java programmer having problems with C.