473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Optimizing code by removing unused lines

momotaro
357 100+
this program is to double space the lines but it works perfectly but there is two "unusfull lines in my point of view since i tried every possible version to see if the any porblem will hapen if omited but there was none...could we just take them out?"

this is the code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define LINE_LEN 80
  6. #define NAME_LEN 40
  7.  
  8. int main(void)
  9. {
  10.    char line[LINE_LEN], inname[NAME_LEN], outname[NAME_LEN];
  11.    FILE *inp,
  12.     *outp;
  13.    char *status;
  14.    int i = 0;
  15.  
  16.    printf("Name of the input file -> ");
  17.    scanf(" %s", inname);
  18.    printf("Name of the output file -> ");
  19.    scanf(" %s", outname);
  20.  
  21.    inp = fopen("inname.txt", "r");
  22.    outp = fopen("outname.txt", "w");
  23.  
  24.    for(status = fgets(line, LINE_LEN, inp);
  25.        status != 0;
  26.     status = fgets(line, LINE_LEN, inp))
  27.     {
  28.        if(line[strlen(line) - 1] == '\n')  //these are two lines.
  29.           line[strlen(line) - 1] = '\0'; //these are two lines.
  30.        fprintf(outp, "%3d>> %s\n\n", ++i, line);
  31.         }
  32. return (0);
  33. }
  34.  
thx!
Jul 29 '07 #1
5 1425
ilikepython
844 Expert 512MB
this program is to double space the lines but it works perfectly but there is two "unusfull lines in my point of view since i tried every possible version to see if the any porblem will hapen if omited but there was none...could we just take them out?"

this is the code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define LINE_LEN 80
  6. #define NAME_LEN 40
  7.  
  8. int main(void)
  9. {
  10.    char line[LINE_LEN], inname[NAME_LEN], outname[NAME_LEN];
  11.    FILE *inp,
  12.     *outp;
  13.    char *status;
  14.    int i = 0;
  15.  
  16.    printf("Name of the input file -> ");
  17.    scanf(" %s", inname);
  18.    printf("Name of the output file -> ");
  19.    scanf(" %s", outname);
  20.  
  21.    inp = fopen("inname.txt", "r");
  22.    outp = fopen("outname.txt", "w");
  23.  
  24.    for(status = fgets(line, LINE_LEN, inp);
  25.        status != 0;
  26.     status = fgets(line, LINE_LEN, inp))
  27.     {
  28.        if(line[strlen(line) - 1] == '\n')  //these are two lines.
  29.           line[strlen(line) - 1] = '\0'; //these are two lines.
  30.        fprintf(outp, "%3d>> %s\n\n", ++i, line);
  31.         }
  32. return (0);
  33. }
  34.  
thx!
No, they're not necessary. From cplusplus.com:
char * fgets ( char * str, int num, FILE * stream );

Get string from stream

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first.
A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str.

A null character is automatically appended in str after the characters read to signal the end of the C string.
Jul 29 '07 #2
momotaro
357 100+
Iknow all that but whene I'v omited theme it just work as well???
and if we don't take the two lines out if new line character reached it will be \0\0 at the end of the str am I righbt?
and that also is a waste of cells??
Jul 30 '07 #3
sicarie
4,677 Expert Mod 4TB
momotaro-

Please use good thread titles - that's something I would have expected a member with 100+ posts to know....

http://www.thescripts.com/forum/faq....d_thread_title
Jul 30 '07 #4
JosAH
11,448 Expert 8TB
The fgets() function appends the \n character in the buffer before terminating it
with a \0 character, so either leave those lines in and print two \n yourself or
remove them and print just one \n character if you want doubly spaced lines.

kind regards,

Jos
Jul 30 '07 #5
momotaro
357 100+
that is an answer !!! thank you!
Jul 30 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Anon | last post by:
Hello all, I'm hoping for some guidance here... I am a c/c++ "expert", but a complete python virgin. I'm trying to create a program that loads in the entire FreeDB database (excluding the...
4
by: J. Campbell | last post by:
From reading this forum, it is my understanding that C++ doesn't require the compiler to keep code that does not manifest itself in any way to the user. For example, in the following: { for(int...
35
by: Geronimo W. Christ Esq | last post by:
Are there any scripts or tools out there that could look recursively through a group of C/C++ source files, and allow unreferenced function calls or values to be easily identified ? LXR is handy...
3
by: Nick Gilbert | last post by:
Hi, I have to send an array of prices for a list of products over XML. Currently my XML data looks like this: <ArrayOfProd> <Prod Code="productcode001"> <Prices> <P F="2005-01-01"...
2
by: Jack | last post by:
I have a chunk of code that loads a few dozen function pointers into global variables. I'm concerned with unused memory consumption. What if the client only needs to use one or two functions? Then...
5
by: Cleverbum | last post by:
I'm not really accustomed to string manipulation and so I was wondering if any of you could be any help i speeding up this script intended to change the format of some saved log information into a...
0
by: schwehr | last post by:
Hi All, I've got a small logging class that I would like to have a way to optimize away as much as possible with the minimum of macro trickery. I've got working code, but not having done C++...
19
by: Pavan | last post by:
Hi, I want to know if there is any software for measuring lines of code of my c++ application. I found out a tool, sloccount, but it gives only physical lines of code. I found out one more...
27
by: George2 | last post by:
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.