LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-27-2005, 03:13 PM   #1
realos
Member
 
Registered: Jul 2002
Location: Germany
Distribution: Redhat 7.3, Debian 3.1, Knoppix 3.1, Ubuntu 6.10
Posts: 113

Rep: Reputation: 15
Reading and writing to files not working proporly


I like the following simple programm to manipulate text files but seem to contain a few
mistakes.

The reading a file (readfile function) works fine except for a bug (marked in the file with a preprozessor
#ifdef_BUG).

Writing to the file (writefile function) does not work.

I have been trying to fix errors for quite a while. Hope someone with more c experience sees
the mistakes.

Q2) I noticed in man page of getline() the "#define _GNU_SOURCE". Is it recommended to
include it in my programmes when using respective function?



Thank you very much in anticipation.

regards,

Realos,



Please find execution "script" of programm followed by the source code below. Sorry for copying whole programm here, but I really do not understand where exactly the mistake is. (maybe in getline() usage?)



Program execution: 1st try with file_modus = r
realos@localpc:~/>./read_write_file
************************* MENU *************************

File name: /tmp/testfile
Entered dummy_character:
Choose file modus r = (read),a = (append/write): r
in readfile() ...

*********************************************************************
this is the first line
second
third
and the last one for time being
********************************************************************
readfile() executed
Programm ends here!


Program execution: 2nd try with file_modus a
realos@localpc:~/> ./read_write_file
************************* MENU *************************

File name: /tmp/testfile
Entered dummy_character:
Choose file modus r = (read),a = (append/write): a
In writefile(), getline() executed.
Now it should fputs() the line to stdout and in the file

fputs() to stdout ... done
writefile() executed
Programm ends here!




Code:
Source Code File 

//*********************** QUESTION ********************* ????????????
//**********************************************************************
//man page of "getline()"  stats:
//#define  _GNU_SOURCE
// does it mean, I need to define it?? Or is it just recommended?
//*********************************************************************


#include <stdio.h>
#include <stdlib.h>


// some useful preprozessor definitions during the programm development

#define _READFILE
#define _WRITEFILE
#define _BUG
//#define _RF_WHILE_TEST

char file_name[100];
int file_modus;

int  readfile(const char *);
void writefile(const char *);
void menu();

int main(){
  
   menu();


   printf("Programm ends here!\n");

}

void menu(){
  printf("************************* MENU *************************\n\n");
  printf("File name: \t");
  scanf("%s",&file_name[0]);


#ifdef _BUG
// REMOVE_ME
//*********************** QUESTION ********************* ????????????
//This is a bug ................................. why do I need to add this function here??
 char dummy_character; 
 scanf("%c",&dummy_character); 
 printf("Entered dummy_character: \t%c",dummy_character);
//End REMOVE_ME 
#endif

  printf("Choose file modus r = (read),a = (append/write):\t "); 

   switch(file_modus=getchar()) {
   case 'r': 
     readfile(file_name);
printf("*********************************************************************\n\n");
     printf("readfile() executed\n"); 
     break;
#ifdef _WRITEFILE
   case 'w':
     writefile(file_name);
     printf("writefile() executed\n");
     break;
   default:
     printf("Default is a=(append/write) modus \n");
     writefile(file_name);
#endif
   }

}


#ifdef _READFILE
// return value of readfile() to be taken care of seperately in main() ...later
int readfile(const char * rfile_name){
  FILE * rfd;
  size_t len = 0;
  char * line = NULL;
  ssize_t read;

printf("in readfile() ... \n");

  rfd = fopen(rfile_name, "r");

printf("\n\n*********************************************************************\n");
  if (rfd == NULL){
	  perror("rfd = fopen(\"/tmp/testfile\",\"r\")");
	  return(EXIT_FAILURE);
	  }
  while( (read = getline (&line, &len, rfd)) != -1){

#ifdef _RF_WHILE_TEST
      printf("in while-loop ... \n");

      printf("**************************************************\n");
      printf("read = %i , len = %i\n", read, len);
#endif
      printf("%s",line);
  	}

      if (line)
      free (line);
	
	
	}
#endif

#ifdef _WRITEFILE
void writefile(const char *wfile_name)
{
  FILE * wfd;
  size_t len = 0;
  char * line = NULL;
  ssize_t write;
  int result;

	wfd = fopen(wfile_name, "a"); if (wfd==NULL) perror("fopen()");
//Is the mistake in the followig getline()?
 	result = getline(&line, &len, stdin); if (result == -1) perror("getline()");
     	printf("In writefile(), getline() executed.\n Now it should fputs() the line to stdout and in the file \n");

	result = fputs(line, stdout); if(result ==-1) perror("fputs()");
     	printf("fputs() to stdout ... done\n");

	result = fputs(line, wfd); if(result ==-1) perror("fputs()");


	}

#endif

Last edited by realos; 07-27-2005 at 05:11 PM.
 
Old 07-27-2005, 04:08 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,506

Rep: Reputation: 116Reputation: 116
In the future, please post code between [ code] and [ /code] blocks (without the spaces).

Quote:
Q2) I noticed in man page of getline() the "#define _GNU_SOURCE". Is it recommended to
include it in my programmes when using respective function?
Without a #define _GNU_SOURCE *prior* to including the header file, one of two things will happen: the function in question will not be included, or its behavior may differ from that documented in the man page.
 
Old 07-27-2005, 05:14 PM   #3
realos
Member
 
Registered: Jul 2002
Location: Germany
Distribution: Redhat 7.3, Debian 3.1, Knoppix 3.1, Ubuntu 6.10
Posts: 113

Original Poster
Rep: Reputation: 15
Quote:
Without a #define _GNU_SOURCE *prior* to including the header file, one of two things will happen: the function in question will not be included, or its behavior may differ from that documented in the man page.
Thank you for clarification Matir. That is very helpful.


I will modify my code to enable that macro definition and write here if the other problems vanish.



thanks.

Last edited by realos; 07-27-2005 at 05:16 PM.
 
Old 07-27-2005, 05:19 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,459

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
when is wfile closed ?
 
Old 08-05-2005, 08:16 AM   #5
realos
Member
 
Registered: Jul 2002
Location: Germany
Distribution: Redhat 7.3, Debian 3.1, Knoppix 3.1, Ubuntu 6.10
Posts: 113

Original Poster
Rep: Reputation: 15
you are right, I forgot to close the file but the real problem was somewhere else.
I rewrote the program and have posted a more specific question on linuxquestions accordingly (see http://www.linuxquestions.org/questi...hreadid=348152)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary reading/writing in cpp kornerr Programming 1 11-27-2005 07:09 PM
C File reading and writing AbhishekSamuel Programming 3 05-03-2005 03:59 PM
Help reading and writing to file xiste Programming 1 04-15-2005 12:43 AM
Reading and Writing DVD media elsabio Fedora 2 03-06-2005 06:25 PM
reading / writing OpenOffice files. GŠutama Programming 1 11-04-2003 06:46 AM


All times are GMT -5. The time now is 07:44 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration