473,748 Members | 4,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ow can i use fgets to read and ignore the first two lines of a file and output into another file

Novice programmer needs help with using fgets to read and ignore the
first two lines of a file. I've gone thru the previous posting
regarding fgets, but none of them seems to help my situation.
I have airdata file that i have to read, but in other teh fscanf to
work properly, i need to ignore the first two lines, because scanf does
not read spaces.

This is what my current code looks like

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

int main()
{
FILE * AirFile; //the file that contains the data to be read
FILE *textFile; //the out file for both lines
char text1[1000];
char text2[100]

if (AirFille == NULL || textFile)
{
printf("Failed to open file \n")
return -1;

else if ( text1[0] =='\n')
{ fgets(text1, 1000, AirFile);
fprintf(textFil e, "allocating :", AirFile);
}
}
fclose(textFile );
fclose(AirFile) '

retrun 0;

}

Sep 30 '06 #1
9 3463
Justme wrote:
[...]
This is what my current code looks like
[...]
Please post actual code, not an inaccurate "looks like"
hazy approximation that won't even compile. All the time
spent debugging your `retrun' statement and the other errors
introduced by sloppy transcription is time not spent solving
your actual problem.

--
Eric Sosman
es*****@acm-dot-org.invalid
Sep 30 '06 #2

Eric Sosman wrote:
Justme wrote:
[...]
This is what my current code looks like
[...]

Please post actual code, not an inaccurate "looks like"
hazy approximation that won't even compile. All the time
spent debugging your `retrun' statement and the other errors
introduced by sloppy transcription is time not spent solving
your actual problem.

--
Eric Sosman
es*****@acm-dot-org.invalid

that is my actual code, that is why i'm trying to get help.

Sep 30 '06 #3
Justme wrote:
>
Novice programmer needs help with using fgets to read and ignore the
first two lines of a file. I've gone thru the previous posting
regarding fgets, but none of them seems to help my situation.
I have airdata file that i have to read, but in other teh fscanf to
work properly, i need to ignore the first two lines,
because scanf does
not read spaces.

This is what my current code looks like

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

int main()
{
FILE * AirFile; //the file that contains the data to be read
FILE *textFile; //the out file for both lines

char text1[1000];
char text2[100]

if (AirFille == NULL || textFile)
{
printf("Failed to open file \n")
return -1;

else if ( text1[0] =='\n')
{ fgets(text1, 1000, AirFile);
fprintf(textFil e, "allocating :", AirFile);
}
}
fclose(textFile );
fclose(AirFile) '

retrun 0;

}
That might be a sketch artist's impression of you code,
but that's not what your code looks like.

There is no standard header called <sting.hin C.
There is no "retrun" keyword in C.
Your "else" is not associated with a previous "if".
You don't make any attempt to open the files.

--
pete
Sep 30 '06 #4

"Justme" <he*********@ya hoo.comwrote in message
news:11******** *************@c 28g2000cwb.goog legroups.com...
>
Eric Sosman wrote:
>Justme wrote:
[...]
This is what my current code looks like
[...]

Please post actual code, not an inaccurate "looks like"
hazy approximation that won't even compile. All the time
spent debugging your `retrun' statement and the other errors
introduced by sloppy transcription is time not spent solving
your actual problem.

--
Eric Sosman
es*****@acm-dot-org.invalid


that is my actual code, that is why i'm trying to get help.
Synatx errors are not the same as logic errors, and are usually picked up by
the compiler at compile time. All keywords must be spelt correctly, as must
all identifiers.

As for ignoring two lines, that is no problem. Simply call fgets() twice
with a big buffer, and ignore the result. Strictly you should check for read
errors, but it's probably best to leave that at present.

--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.
Sep 30 '06 #5
Justme wrote:
Eric Sosman wrote:
>>Justme wrote:
>>>[...]
This is what my current code looks like
[...]

Please post actual code, not an inaccurate "looks like"
hazy approximation that won't even compile. All the time
spent debugging your `retrun' statement and the other errors
introduced by sloppy transcription is time not spent solving
your actual problem.

--
Eric Sosman
es*****@acm-dot-org.invalid

that is my actual code, [...]
Then it is beyond my poor powers to debug. Have a
nice life!

--
Eric Sosman
es*****@acm-dot-org.invalid
Oct 1 '06 #6
"Justme" <he*********@ya hoo.comwrote in message
news:11******** *************@c 28g2000cwb.goog legroups.com...
>
Eric Sosman wrote:
Justme wrote:
<snip>
that is my actual code, that is why i'm trying to get help.
Well here's a the code with proper formatting but I havn't done
anything concerning files and can't be bothered looking it up for you
but maybe without all the other errors you'll be able to figure it
out:

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

int main() {

FILE *AirFile; /*the file that contains the data to be read*/
FILE *textFile; /*the out file for both lines*/

char text1[1000];
char text2[100];

if (AirFile == NULL || textFile == NULL) {
printf("Failed to open file \n");
return -1;

if ( text1[0] =='\n') {
fgets(text1, 1000, AirFile);
fprintf(textFil e, "allocating :", AirFile);
}
}

fclose(textFile );
fclose(AirFile) ;
return 0;
}
Oct 1 '06 #7
"Justme" <he*********@ya hoo.comwrites:
Novice programmer needs help with using fgets to read and ignore the
first two lines of a file. I've gone thru the previous posting
regarding fgets, but none of them seems to help my situation.
I have airdata file that i have to read, but in other teh fscanf to
work properly, i need to ignore the first two lines, because scanf does
not read spaces.

This is what my current code looks like

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

int main()
{
FILE * AirFile; //the file that contains the data to be read
FILE *textFile; //the out file for both lines
Files never got opened.
char text1[1000];
char text2[100]

if (AirFille == NULL || textFile)
AirFile and textFile are not initialised thus the condition is true or
false depending on position of the Sun.
{
printf("Failed to open file \n")
return -1;

else if ( text1[0] =='\n')
Unexpected "else". Plus text1[0] is uninitialised (so again condition
depends on position of the stars) plus it (sort of) checks for empty
lines only.
{ fgets(text1, 1000, AirFile);
fprintf(textFil e, "allocating :", AirFile);
}
}
fclose(textFile );
fclose(AirFile) '

retrun 0;

}
Here's the way to do it with stdin/stdout:

#v+
#include <stdio.h>

int main(void) {
char buffer[1024];
size_t num = 2;

while (num && fgets(buffer, sizeof buffer, stdin)) {
const char *ch = buffer;
while (*ch && *ch!='\n') ++ch;
if (*ch) --num;
}

while (!feof(stdin) && !ferror(stdin) &&
(num = fread(buffer, 1, sizeof buffer, stdin)) &&
num == fwrite(buffer, 1, num, stdout));

return 0;
}
#v-

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl >--<jid:mina86*jab ber.org>--ooO--(_)--Ooo--
Oct 1 '06 #8
Michal Nazarewicz said:

<snip>
Here's the way to do it with stdin/stdout:

#v+
No, that's just a syntax error.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 1 '06 #9
Michal Nazarewicz wrote:
"Justme" <he*********@ya hoo.comwrites:
Novice programmer needs help with using fgets to read and ignore the
first two lines of a file. I've gone thru the previous posting
regarding fgets, but none of them seems to help my situation.
I have airdata file that i have to read, but in other teh fscanf to
work properly, i need to ignore the first two lines, because scanf does
not read spaces.

while (num && fgets(buffer, sizeof buffer, stdin)) {
const char *ch = buffer;
while (*ch && *ch!='\n') ++ch;
if (*ch) --num;
}

while (!feof(stdin) && !ferror(stdin) &&
(num = fread(buffer, 1, sizeof buffer, stdin)) &&
num == fwrite(buffer, 1, num, stdout));
Really no need to make it so complicated for 2 initial lines... i.e.
fread() is already heading towards pre-optimization territory, where
you might as well use mmap() (not ansi C however) if you have it.

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

int main(int argc, char **argv)
{
FILE *in;
char buf[128];
size_t i;

if (argc <= 1 || (in = fopen(argv[1], "r")) == NULL)
return -1;

for (i = 0; i < 2 && fgets(buf, sizeof(buf), in); )
if (strchr(buf, '\n')) i++;

while (fgets(buf, sizeof(buf), in)) {
/* won't get here unless we've read 2 lines */
fprintf(stdout, "%s", buf);
}

fclose(in);

return 0;
}

Oct 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7245
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files, two aspects which I want to incorporate into my program eventually. That aside, my most pressing problem right now is how to get rid of the newline in the input when I use fgets(). Now I have looked around on the net, not so much in this group...
20
5680
by: Paul D. Boyle | last post by:
Hi all, There was a recent thread in this group which talked about the shortcomings of fgets(). I decided to try my hand at writing a replacement for fgets() using fgetc() and realloc() to read a line of arbitrary length. I know that the better programmers in this group could write a more robust function, but here is my shot at it anyway. I would appreciate people's comments on my fget_line() code below (usage example included). Any...
8
558
by: Magix | last post by:
Hi, I'm not too sure on reading specify string with the first < > from file stream,and make the file pointer go to next line, with fgets. or any better idea. E.g let say text.txt has <11111><Joe><aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> <22222222><Ron><bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb>
20
7905
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The standard says about fgets: synopsis #include <stdio.h> char *fgets(char *s, int n, FILE *stream);
35
9983
by: David Mathog | last post by:
Every so often one of my fgets() based programs encounters an input file containing embedded nulls. fgets is happy to read these but the embedded nulls subsequently cause problems elsewhere in the program. Since fgets() doesn't return the number of characters read it is pretty tough to handle the embedded nulls once they are in the buffer. So two questions: 1. Why did the folks who wrote fgets() have a successful
42
6814
by: mellyshum123 | last post by:
I need to read in a comma separated file, and for this I was going to use fgets. I was reading about it at http://www.cplusplus.com/ref/ and I noticed that the document said: "Reads characters from stream and stores them in string until (num -1) characters have been read or a newline or EOF character is reached, whichever comes first." My question is that if it stops at a new line character (LF?) then how does one read a file with...
20
11018
by: Xavoux | last post by:
Hello all... I can't remind which function to use for safe inputs... gets, fgets, scanf leads to buffer overflow... i compiled that code with gcc version 2.95.2, on windows 2000 char tmp0 = "ABCDEFGHI\0"; char buff; /* Input buffer. */ char tmp1 = "ABCDEFGHI\0";
9
3422
by: uidzer0 | last post by:
Hey everyone, Taken the following code; is there a "proper" or dynamic way to allocate the length of line? #include <stdio.h> #include <errno.h> int main(int argc, char **argv) { FILE *fp;
24
2963
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be modified and the program code. In the attached file below i just want to change the value of data(only float value) after the line 1 P V T 1 15 till 2 G TT, from positive to negative and vice versa, and wire the date in other file. can someone help...
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.