Jump to content

I need help reading from and writing to files (C)

FaiL___

So, I've been trying to (just at the minute) print out a text file line by line. I will eventually need to get it reading into an array, but I'll be able to do that once I get the printout to work. I haven't even attempted writing to it yet, but here's what I have so far...

void ReadToArrayFromTextFile()
{
	FILE *fptr;
	fptr = fopen("keys.txt", "r");
	char line[256];

	while (fgets(line, sizeof(line), fptr))
	{
		printf("%s", line);
	}
	fclose(fptr);
}

 

There are 10 types of people in the world. Those that understand binary and those that don't.

Link to comment
Share on other sites

Link to post
Share on other sites

What issue are you having? Are you just asking for help on writing, or are you getting incorrect behavior when reading?

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, reniat said:

What issue are you having? Are you just asking for help on writing, or are you getting incorrect behavior when reading?

When I call the function, nothing get's printed. Sorry, I thought i said that, was pretty tired writing this post!

There are 10 types of people in the world. Those that understand binary and those that don't.

Link to comment
Share on other sites

Link to post
Share on other sites

I've got a few notes, partly just about coding practices.

 

1) Get in the habit of defining your variables.  It saves headaches.  For example:

char line[256] = {0};

2) The while should be an if-else, and you should give output in case it fails:

if (fgets(line, sizeof(line), fptr) != NULL)
{
	printf("%s\n", line);
} else {
	printf("Error: failed to read file\n");
}

3) In situations like this, I suggest using a macro I wrote for debugging.  Add this below the includes:

#include <stdio.h>
#include <stdarg.h>

#define ENABLE_DEBUG

#ifdef ENABLE_DEBUG
void debug(const char *format, ...) {
	va_list args;
	va_start(args, format);
	vprintf(format, args);
	va_end(args);
}
#else
#define debug(...)
#endif

You can now use the "debug" function just like printf.  If you comment out

#define ENABLE_DEBUG

then all "debug" functions will be ignored by the compiler.  It's a great way to give a bunch of output for testing, while allowing you to take it all out with two forward slashes.

 

If you don't want the error to show up in the final product, you can do this:

if (fgets(line, sizeof(line), fptr) != NULL)
{
	printf("%s\n", line);
} else {
	debug("Error: failed to read file\n");
}

If "keys.txt" doesn't exist, you'll only see an error if ENABLE_DEBUG is defined.

 

My guess is that it's failing to open keys.txt.

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
Share on other sites

Link to post
Share on other sites

On 3/22/2018 at 4:35 PM, FaiL___ said:

So, I've been trying to (just at the minute) print out a text file line by line. I will eventually need to get it reading into an array, but I'll be able to do that once I get the printout to work. I haven't even attempted writing to it yet, but here's what I have so far...


void ReadToArrayFromTextFile()
{
	FILE *fptr;
	fptr = fopen("keys.txt", "r");
	char line[256];

	while (fgets(line, sizeof(line), fptr))
	{
		printf("%s", line);
	}
	fclose(fptr);
}

 

1)  Make sure the file you use for testing this is guaranteed to work.  This is of course, unrealistic for a working program, but for testing the code and getting it to work, it's fine.

In your case, since your buffer is at most 255 chars + EOS mark, make sure each line is less than 255 characters.

 

2)  Make sure you're in the correct directory.  I'd recommend commenting out everything in the function, and just run put in place something like

void ReadToArrayFromTextFile()
{
	FILE *fptr;
  
	fptr = fopen("TestFile_FindMe.txt", "w");
  
	//fptr = fopen("keys.txt", "r");
	// char line[256];

	//while (fgets(line, sizeof(line), fptr))
	//{
	//	printf("%s", line);
	//}
	fclose(fptr);
}

Then make sure the keys.txt file is in the same directory as the TestFile_FindMe.txt file.

 

3)  I'd recommend adding in some more checks.

For instance, after opening the file, check for the pointer to be null.  If it is, then you know it failed to open the file at all, instead of it being an error during the reads.

 

4)  And I'd also recommend in the while loop going ahead and being more specific in the condition.

while(fgets(...) != NULL)

 

Probably not necessary, but the goal is to limit sources of error.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×