Sponsored Content
Top Forums Programming Need ideas how to attack this problem Post 302121575 by frequency8 on Saturday 16th of June 2007 01:43:47 PM
Old 06-16-2007
Quote:
Originally Posted by grumpf
hi frequency8,
i am not sure if i understand you problem totaly

1. add an \n when missing
ask your self when can this happen ? perhaps at the last line in file ?

2. Skip empty lines, i asume that empty means 'contains no char execpt \n'
why not compare this line with '\n' ?

3. to get extra points use gnu- getline() function. It saves you thinking about 'did i read the whole line or only part of it because my buffer is to small ?'

Regarding point 1. I would assume '\n' would be missing if it was the last line in the file. I still have no idea how to solve it in this case.

Regarding point 2. I compared the line with '\n'. It appears to work correctly. Here is what I have
[cda@localhost ~]$ more file.txt
a

b



c
d

Code:
#include <stdio.h>
#include <string.h>

#define LINE_LENGTH 80

main(void)
{
   FILE* fp = NULL;
   static char line[LINE_LENGTH];
   char key[] = "\n";

   fp=fopen("/home/cda/file.txt","r");
   while ( fgets(line, LINE_LENGTH, fp) != NULL) {
     if((strcmp (line,key) == 0)) {
       continue;
     }
     if((strlen(line)) < LINE_LENGTH)
       printf("File contains %s", line);
     else {
       printf("The input line is too long\n");
     }
   }

   fclose(fp);
   return 0;
}

Regarding point 3. I have getline() on my Linux box. However, the target systems that I would be running this under use FreeBSD and OpenBSD. I'm not sure if getline() is available on these systems.
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Any Ideas?

I have several staging directories on my UNIX server. /usr2/data1 /usr2/data2 /usr2/data3 /usr2/data4 /usr2/data5 /usr2/data6 In these directories a file is transferred from different PC's connected to the network via TCP/IP. The File name is constant for all directories. Transfers... (1 Reply)
Discussion started by: Docboyeee
1 Replies

2. Cybersecurity

Any Ideas !!!!!!!!!!

Hi, I installed sybase server on a LINUX server. I assigen port 2025 whilst installation for sybase , later i uninstalled sybase and when i try to reinstall sybase and use port 2025 it throw up error saying that it is already in use, use other port number. How can I re-use the same port number... (2 Replies)
Discussion started by: suda
2 Replies

3. Shell Programming and Scripting

any ideas?

i need to compare to dates/times given in the format MMDDhhmmYY. That is month, day, hour, minute, year. It is a 24 hour clock. I need to compare two dates to check that they are, say, less than 900 seconds apart. I have got to a point where it checks the time, turns the values into seconds and... (5 Replies)
Discussion started by: fwabbly
5 Replies

4. Shell Programming and Scripting

Problem with Script that writes max lines of a file - Any ideas how to fix?

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (4 Replies)
Discussion started by: mmiller99
4 Replies

5. UNIX for Advanced & Expert Users

Sendmail - Any Ideas?

I have solaris 9 and am using sendmail to pickup requests and forward them to a bulk mail server on a different port. Now for the fun...In sendmail, I need to find a way to place a default address in the Mail From: and Rcpt To: or remove them as required - These will be picked up by the bulk mail... (3 Replies)
Discussion started by: mikey2003ma
3 Replies

6. Shell Programming and Scripting

BASH script problem using find, ideas?

Hi, I'm trying to write a script to search through my computer and find all .jpg files and put them all in a directory. So far I have this: for i in `find /home -name '*.jpg' ` ; do mv $i home/allen/Pictures/PicturesFound ; done When I run it, I get this error (this is only part of it, it... (2 Replies)
Discussion started by: FortressPTH
2 Replies

7. UNIX and Linux Applications

Need ideas for graduation project based on unix or linux Need ideas for graduation project based on

Dear all, i am in last year of electronics department in engineering faculty i need suggestions for a graduation project based on unix or free bsd or linux and electronics "embedded linux " i think about embedded unix for example or device drivers please i need helps (1 Reply)
Discussion started by: MOHA-1
1 Replies

8. Ubuntu

Problem in Postfix server/is my server got some attack

Hi Friends, This is logs of my mail log: mail for yahoo.com.tw is using up 4001 of 6992 active queue entries : 1 Time(s) mail for yahoo.com.tw is using up 4001 of 7018 active queue entries : 1 Time(s) mail for yahoo.com.tw is using up 4001 of 7072 active queue entries : 1 Time(s) ... (1 Reply)
Discussion started by: darakas
1 Replies

9. Shell Programming and Scripting

Help me get some ideas

Hello, I have been given a scripting project, but have not learned any scripting. I need to get some ideas on how to start. Attached is part of the project: I have no idea how to parse the arguments. What I had in mind was to get the arguments ($1, $2, ... ) and have if statements for different... (1 Reply)
Discussion started by: facepalm
1 Replies
GETLINE(3)						     Linux Programmer's Manual							GETLINE(3)

NAME
getline, getdelim - delimited string input SYNOPSIS
#include <stdio.h> ssize_t getline(char **lineptr, size_t *n, FILE *stream); ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): Before glibc 2.10: getline(), getdelim(): _GNU_SOURCE Since glibc 2.10: getline(), getdelim(): _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700 DESCRIPTION
getline() reads an entire line from stream, storing the address of the buffer containing the text into *lineptr. The buffer is null-termi- nated and includes the newline character, if one was found. If *lineptr is NULL, then getline() will allocate a buffer for storing the line, which should be freed by the user program. (In this case, the value in *n is ignored.) Alternatively, before calling getline(), *lineptr can contain a pointer to a malloc(3)-allocated buffer *n bytes in size. If the buffer is not large enough to hold the line, getline() resizes it with realloc(3), updating *lineptr and *n as necessary. In either case, on a successful call, *lineptr and *n will be updated to reflect the buffer address and allocated size respectively. getdelim() works like getline(), except a line delimiter other than newline can be specified as the delimiter argument. As with getline(), a delimiter character is not added if one was not present in the input before end of file was reached. RETURN VALUE
On success, getline() and getdelim() return the number of characters read, including the delimiter character, but not including the termi- nating null byte. This value can be used to handle embedded null bytes in the line read. Both functions return -1 on failure to read a line (including end-of-file condition). ERRORS
EINVAL Bad arguments (n or lineptr is NULL, or stream is not valid). VERSIONS
These functions are available since libc 4.6.27. CONFORMING TO
Both getline() and getdelim() were originally GNU extensions. They were standardized in POSIX.1-2008. EXAMPLE
#define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> int main(void) { FILE *fp; char *line = NULL; size_t len = 0; ssize_t read; fp = fopen("/etc/motd", "r"); if (fp == NULL) exit(EXIT_FAILURE); while ((read = getline(&line, &len, fp)) != -1) { printf("Retrieved line of length %zu : ", read); printf("%s", line); } free(line); exit(EXIT_SUCCESS); } SEE ALSO
read(2), fgets(3), fopen(3), fread(3), gets(3), scanf(3), feature_test_macros(7) COLOPHON
This page is part of release 3.25 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU
2010-06-12 GETLINE(3)
All times are GMT -4. The time now is 10:34 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy