LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-18-2013, 12:46 AM   #1
madhuhan
LQ Newbie
 
Registered: Mar 2013
Posts: 4

Rep: Reputation: Disabled
how to movw to the next line


Hello all,
M new to coding.. n i have this task wherein i m suppose to write a code to print every field in a file with a printf statement .here i swhat i have done so far. my problem is that i m not able to figure out how to move to the next line. please help me with this.#include <stdio.h>
//#define EOF 0
int main()
{
// open the text file "fred.txt" for writing

// close the stream, so all changes to the file are saved

// open the file "fred.txt" for reading
FILE *in = fopen("kerg3470.03o", "rt");
// read the first line from the file
char buffer[100];
fgets(buffer, 20, in);
//display what we've just read
printf("The rinex version of:\"hanu.c\": %s\n", buffer);
fgets( buffer, 23 , in);
printf("The type of:\"hanu.c\": %s\n", buffer);

fgets( buffer, 40 , in);

printf("The type of:\"kerg3470.03o\": %s\n", buffer);
while(in==EOF)
fgets( buffer, 50, in);
printf("The type of:\"kerg3470.03o\": %s\n", buffer);
// close the stream
fclose(in);
return 0;
}
 
Old 03-18-2013, 12:50 AM   #2
madhuhan
LQ Newbie
 
Registered: Mar 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
[QUOTE=madhuhan;4913622]Hello all,
M new to coding.. n i have this task wherein i m suppose to write a code to print every field in a file with a printf statement .here i swhat i have done so far. my problem is that i m not able to figure out how to move to the next line. please help me with this.#include <stdio.h>
//#define EOF 0
int main()
{
// open the text file "hanu.c" for writing

// close the stream, so all changes to the file are saved

// open the file "fred.txt" for reading
FILE *in = fopen("hanu.c", "rt");
// read the first line from the file
char buffer[100];
fgets(buffer, 20, in);
//display what we've just read
printf("The rinex version of:\"hanu.c\": %s\n", buffer);
fgets( buffer, 23 , in);
printf("The type of:\"hanu.c\": %s\n", buffer);

fgets( buffer, 40 , in);

printf("The type of:\"hanu.c\": %s\n", buffer);
while(in==EOF)
fgets( buffer, 50, in);
printf("The type of:\"hanu.c\": %s\n", buffer);
// close the stream
fclose(in);
return 0;
}
 
Old 03-18-2013, 01:49 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
You could see eg http://stackoverflow.com/questions/2...he-line-length : note the comments on the getline() fn.
Fundamentally you need a loop of eg fgets() or whatever input fn you use.
Google has millions of results on this eg also read http://stackoverflow.com/questions/4...ne-using-fgetc
 
1 members found this post helpful.
Old 03-19-2013, 11:40 PM   #4
madhuhan
LQ Newbie
 
Registered: Mar 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
thanks a ton ! now i have another problem.. could u pls help with as to how to skip reading lines in btw while using fscnf statements.. and print only selected data...
thanks in advance....
 
Old 03-20-2013, 12:28 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Mate, seriously just google it; there are more C tutorials on the web than you could throw a compiler at
Incidentally, you might also ask the Mods (via the Report button) to move this to the Programming forum.

Please always use code tags https://www.linuxquestions.org/quest...do=bbcode#code to post your code and use proper English, not 'could u pls help '.

Thank you.
 
Old 03-20-2013, 06:49 AM   #6
madhuhan
LQ Newbie
 
Registered: Mar 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Smile how do i print specific data from a file

Hey there is a file...
223423 45435 hjhsdfkjhdskjclk aaaaaaaaaaaaa
djdckjdxlclklk bbbbbbbbbbbbb
djhsbxjhABN hhhhhhhhhhhhh
465346 989374839 9090
73982 879378 80980
sorry for the random data :P.. anyway
i need to print only the first line ignore aaaaaa... and print the fourth line after the first one.. how do i skip those line and ignore aaaa... in the same line...
looking forward for the replies...

Last edited by madhuhan; 03-20-2013 at 06:50 AM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Cursor does not move to the bottom line and overwrite to same line on command line mesuutt Linux - Newbie 3 02-25-2012 09:04 AM
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 11:48 AM
[SOLVED] Copy and replacing specific line from file1 to file2 line by line vjramana Programming 10 03-28-2011 08:49 AM


All times are GMT -5. The time now is 08:33 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration