10 Replies - 244 Views - Last Post: 03 April 2012 - 11:43 AM Rate Topic: -----

#1 chiguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 17-October 10

Printing a line from a file onto the screen.

Posted 03 April 2012 - 10:50 AM

Hi everyone. I'm having trouble figuring out why my code doesn't work. I'm just trying to print a line from a file onto the screen. This is for part of a larger project. Right now it prints nothing. I think it has to do with how I'm handling the line char* but I'm not 100% sure. It opens the file apparently but it doesn't read in the lines correctly, I'm not really sure. I tried two different ways of printing it in the code.

Thanks for the help in advance!!

P.S. this is C not C++ :)

        FILE *fp;

	char line[500];        //the line we are reading in
	
	int current_line = 0;  //current line loop is at

	char* file;            //filename

	char mychar; int i = 1; //for traversal purposes

	strcpy(file,prog_name); 

	strcat(file, ".c");    //prog_name is a global so we dont want to edit that

	fp = fopen(prog_name, "r");

	if(fp == NULL)
	{
		printf("error reading file bro");
	}
	else
	{
		while(fgets(line, 500, fp) != NULL && current_line < line_num) 
		{
			current_line++;
		}
		mychar = line[0];
		while (mychar != 0)
		{
			printf("%c", line[i]); i++;
		}	
		printf("%s \n", line);
	}

	fclose(fp);


This post has been edited by chiguy: 03 April 2012 - 10:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Printing a line from a file onto the screen.

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 3049
  • View blog
  • Posts: 9,289
  • Joined: 25-December 09

Re: Printing a line from a file onto the screen.

Posted 03 April 2012 - 11:08 AM

Have you tried writing to the screen what you read in you read loop? Like:
	while(fgets(line, 500, fp) != NULL && current_line < line_num) 
	{
                printf("%s\n", line);

		current_line++;
	}


That should tell you if you read the file properly into your string.

Jim

This post has been edited by jimblumberg: 03 April 2012 - 11:08 AM

Was This Post Helpful? 0
  • +
  • -

#3 chiguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 17-October 10

Re: Printing a line from a file onto the screen.

Posted 03 April 2012 - 11:10 AM

View Postjimblumberg, on 03 April 2012 - 11:08 AM, said:

Have you tried writing to the screen what you read in you read loop? Like:
	while(fgets(line, 500, fp) != NULL && current_line < line_num) 
	{
                printf("%s\n", line);

		current_line++;
	}


That should tell you if you read the file properly into your string.

Jim



Just tried that, I got the following:


����-��U
         �T��T$�$�������U������� �D$
�D$

�D$�|$	~��D$










5
�




Clearly this is wrong .... Not really sure what to do :\

This post has been edited by chiguy: 03 April 2012 - 11:10 AM

Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 390
  • View blog
  • Posts: 1,348
  • Joined: 31-December 10

Re: Printing a line from a file onto the screen.

Posted 03 April 2012 - 11:12 AM

Could you post the updated code?
Was This Post Helpful? 0
  • +
  • -

#5 chiguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 17-October 10

Re: Printing a line from a file onto the screen.

Posted 03 April 2012 - 11:14 AM

Sure, sorry. Here it is. Thanks

FILE *fp;

	char line[500];

	char* myline;
	
	int current_line = 0;

	char* file;

	char mychar; int i = 1;

	strcpy(file,prog_name);

	strcat(file, ".c");

	fp = fopen(prog_name, "r");

	if(fp == NULL)
	{
		printf("error reading file bro");
	}
	else
	{
		myline = fgets(line, 500, fp);
		while(myline != NULL && current_line < line_num) 
		{
			myline = fgets(line, 500, fp);
			printf("%s\n", line);
			current_line++;
		}
		printf("%s \n", myline);
	}

	fclose(fp); 

Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 390
  • View blog
  • Posts: 1,348
  • Joined: 31-December 10

Re: Printing a line from a file onto the screen.

Posted 03 April 2012 - 11:16 AM

That while-loop isn't exactly the same as what jimblumberg posted above is it?
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon

Reputation: 3049
  • View blog
  • Posts: 9,289
  • Joined: 25-December 09

Re: Printing a line from a file onto the screen.

Posted 03 April 2012 - 11:17 AM

You need to allocate some memory for your file variable before you can place anything into it.
char* file;            //filename

char mychar; int i = 1; //for traversal purposes

strcpy(file,prog_name); 


Where and how is prog_name defined?

Please provide a complete program that illustrates your problem.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Printing a line from a file onto the screen.

Posted 03 April 2012 - 11:17 AM

You have to declare file as a char array big enough to hold the entire string including the extra .c and the terminating null char, not just a char* pointer.

And then, in fopen(), isn't file the complete filename that you want to use, rather than prog_name?

This post has been edited by r.stiltskin: 03 April 2012 - 11:19 AM

Was This Post Helpful? 1
  • +
  • -

#9 chiguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 17-October 10

Re: Printing a line from a file onto the screen.

Posted 03 April 2012 - 11:26 AM

View Postvividexstance, on 03 April 2012 - 11:16 AM, said:

That while-loop isn't exactly the same as what jimblumberg posted above is it?


No it's not you're right- I will update it and try again. Thanks

View Postjimblumberg, on 03 April 2012 - 11:17 AM, said:

You need to allocate some memory for your file variable before you can place anything into it.
char* file;            //filename

char mychar; int i = 1; //for traversal purposes

strcpy(file,prog_name); 


Where and how is prog_name defined?

Please provide a complete program that illustrates your problem.

Jim


Ok thanks I'll try to allocate some memory for file. prog_name is a global variable that holds the program's name (minus the .c part) The complete program is quite large- it's a debugger project I'm working on. This portion prints a specific line of code from the .c file


View Postr.stiltskin, on 03 April 2012 - 11:17 AM, said:

You have to declare file as a char array big enough to hold the entire string including the extra .c and the terminating null char, not just a char* pointer.


Thanks I'll try that and post the result

I have updated the code to this:

FILE *fp;

	char line[500];

	char* myline;
	
	int current_line = 0;

	char file[500];

	char mychar; int i = 1;

	strcpy(file,prog_name);

	strcat(file, ".c");

	fp = fopen(prog_name, "r");

	if(fp == NULL)
	{
		printf("error reading file bro");
	}
	else
	{
		//myline = fgets(line, 500, fp);
		while(fgets(line, 500, fp) != NULL && current_line < line_num)
		{
		           printf("%s\n", line);
			   current_line++;
		}

		//printf("%s \n", myline);
	}

	fclose(fp);



And the output remains the same....

ELF

����-��U
         �T��T$�$�������U������� �D$
�D$

�D$�|$	~��D$










5
�



View Postr.stiltskin, on 03 April 2012 - 11:17 AM, said:

And then, in fopen(), isn't file the complete filename that you want to use, rather than prog_name?



Wow, that was the problem!! Thank you for catching that, it works now. Awesome!
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon

Reputation: 3049
  • View blog
  • Posts: 9,289
  • Joined: 25-December 09

Re: Printing a line from a file onto the screen.

Posted 03 April 2012 - 11:28 AM

What is the purpose of your file variable? Why do you try to create your file variable but then use prog_name to open the file?

That "ELF" in your output tells me you are opening the executable program not the source file.

Jim

This post has been edited by jimblumberg: 03 April 2012 - 11:29 AM

Was This Post Helpful? 1
  • +
  • -

#11 chiguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 17-October 10

Re: Printing a line from a file onto the screen.

Posted 03 April 2012 - 11:43 AM

View Postjimblumberg, on 03 April 2012 - 11:28 AM, said:

What is the purpose of your file variable? Why do you try to create your file variable but then use prog_name to open the file?

That "ELF" in your output tells me you are opening the executable program not the source file.

Jim


That was the mistake... prog_name is the executable file that's being debugged. So by adding ".c" we can get the .c file and access the code. So that's why I'm using "file" to represent the source file- without editing prog_name as it is a global variable.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1