Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming question for school.
New Posts  All Forums:Forum Nav:

Programming question for school.

post #1 of 3
Thread Starter 
Im writing another program that involves reading 5 records that have 4 lines of text each from a .dat file.

The functions I have that are for reading the 5 records are read_double() and read_integer().

Instructions are to use a combination of those functions to read the information in the 5 records which are 2 ints and 2 doubles each coming out to 4 lines of data.

So far I have gotten to the point of just opening the file for reading, and then failed when trying to get it to read the records.
Quote:
these are the 2 functions:
double read_double (FILE *infile)
{
double double_num = 0.0;

fscanf (infile, "%lf", &double_num);

return double_num;
}

int read_integer (FILE *infile)
{
int integer_num = 0;

fscanf (infile, "%d", &integer_num);

return integer_num;

}
///////////////////And now this is what I have in my main. I got rid of my previous attempt at this part

int main (void)
{
FILE *input_file = NULL;
FILE *output_file = NULL;




input_file = fopen ("input.dat", "r");
output_file = fopen ("output.dat", "w");

return 0;
}

Im lost bit, and hoping someone on here can help me before I seek further help from my professor, etc.
My Current System
(20 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 4770k Haswell @ 4.2GHZ 1.22v MSi Z97 Gaming 5 MSi Gaming X GTX1070 8GB @ 2GHZ G. Skill Trident X 16gb (4x4GB) 2400mhz 1.65v 
Hard DriveHard DriveHard DriveCooling
Samsung 830 128GB SSD  Corsair Neutron 240GB SSD Seagate Barracuda 2TB SATA 6gb/s 64mb cache (2x... EK Supremacy  
CoolingCoolingCoolingOS
Alphacool XT45 360 Radiator Alphacool UT60 240 Radiator MCP350 w/ EK reservior top 140 multi tube Windows 10 Pro 64-Bit 
MonitorKeyboardPowerCase
Qnix QN2710LED 27" 2560 x 1440 PLS @ 100hz Corsair K70 w/ Red Backlit and Cherry MX Browns Corsair HX850 Gen1 Silver Corsair Obsidian 750D 
MouseMouse PadAudioAudio
Steelseries Rival 300 Corsair Mat Realtek ALC1150 w/ MSi Soundboost Hyper X Cloud Pro Headset 
  hide details  
Reply
My Current System
(20 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 4770k Haswell @ 4.2GHZ 1.22v MSi Z97 Gaming 5 MSi Gaming X GTX1070 8GB @ 2GHZ G. Skill Trident X 16gb (4x4GB) 2400mhz 1.65v 
Hard DriveHard DriveHard DriveCooling
Samsung 830 128GB SSD  Corsair Neutron 240GB SSD Seagate Barracuda 2TB SATA 6gb/s 64mb cache (2x... EK Supremacy  
CoolingCoolingCoolingOS
Alphacool XT45 360 Radiator Alphacool UT60 240 Radiator MCP350 w/ EK reservior top 140 multi tube Windows 10 Pro 64-Bit 
MonitorKeyboardPowerCase
Qnix QN2710LED 27" 2560 x 1440 PLS @ 100hz Corsair K70 w/ Red Backlit and Cherry MX Browns Corsair HX850 Gen1 Silver Corsair Obsidian 750D 
MouseMouse PadAudioAudio
Steelseries Rival 300 Corsair Mat Realtek ALC1150 w/ MSi Soundboost Hyper X Cloud Pro Headset 
  hide details  
Reply
post #2 of 3
Thread Starter 
Wait, I think I might have found the issue. In the function declaration for the Reads says (infile) and im thinking in the int main (void) function I need to change the FILE *input_file = NULL; ................to FILE *infile = NULL;

and then do infile = fopen("input.dat", "r");
My Current System
(20 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 4770k Haswell @ 4.2GHZ 1.22v MSi Z97 Gaming 5 MSi Gaming X GTX1070 8GB @ 2GHZ G. Skill Trident X 16gb (4x4GB) 2400mhz 1.65v 
Hard DriveHard DriveHard DriveCooling
Samsung 830 128GB SSD  Corsair Neutron 240GB SSD Seagate Barracuda 2TB SATA 6gb/s 64mb cache (2x... EK Supremacy  
CoolingCoolingCoolingOS
Alphacool XT45 360 Radiator Alphacool UT60 240 Radiator MCP350 w/ EK reservior top 140 multi tube Windows 10 Pro 64-Bit 
MonitorKeyboardPowerCase
Qnix QN2710LED 27" 2560 x 1440 PLS @ 100hz Corsair K70 w/ Red Backlit and Cherry MX Browns Corsair HX850 Gen1 Silver Corsair Obsidian 750D 
MouseMouse PadAudioAudio
Steelseries Rival 300 Corsair Mat Realtek ALC1150 w/ MSi Soundboost Hyper X Cloud Pro Headset 
  hide details  
Reply
My Current System
(20 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 4770k Haswell @ 4.2GHZ 1.22v MSi Z97 Gaming 5 MSi Gaming X GTX1070 8GB @ 2GHZ G. Skill Trident X 16gb (4x4GB) 2400mhz 1.65v 
Hard DriveHard DriveHard DriveCooling
Samsung 830 128GB SSD  Corsair Neutron 240GB SSD Seagate Barracuda 2TB SATA 6gb/s 64mb cache (2x... EK Supremacy  
CoolingCoolingCoolingOS
Alphacool XT45 360 Radiator Alphacool UT60 240 Radiator MCP350 w/ EK reservior top 140 multi tube Windows 10 Pro 64-Bit 
MonitorKeyboardPowerCase
Qnix QN2710LED 27" 2560 x 1440 PLS @ 100hz Corsair K70 w/ Red Backlit and Cherry MX Browns Corsair HX850 Gen1 Silver Corsair Obsidian 750D 
MouseMouse PadAudioAudio
Steelseries Rival 300 Corsair Mat Realtek ALC1150 w/ MSi Soundboost Hyper X Cloud Pro Headset 
  hide details  
Reply
post #3 of 3
Did you ever get this solved? You're not actually ever calling the reading functions. You're also not checking whether fopen was successful, so you can't know whether that FILE pointer is still null or not.
Quote:
Originally Posted by MxPhenom 216 View Post

Wait, I think I might have found the issue. In the function declaration for the Reads says (infile) and im thinking in the int main (void) function I need to change the FILE *input_file = NULL; ................to FILE *infile = NULL;

and then do infile = fopen("input.dat", "r");

The variable names don't matter between scopes like that. 'infile' will be used in the the two functions locally. You can use whatever variable name you want in main for the pointers, and then feed those into the read functions.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming question for school.