i am required to code in C but i have limited knowledge.
Can i read a certain number of lines in a input file, then stop reading and do some functions, then continue to read from the point where i stop by calling a method to read again?
5 1985
i am required to code in C but i have limited knowledge.
Can i read a certain number of lines in a input file, then stop reading and do some functions, then continue to read from the point where i stop by calling a method to read again?
Yes. Your input stream or input buffer will remember where it is as long as you have set up a pointer to the data.
Yes. Your input stream or input buffer will remember where it is as long as you have set up a pointer to the data.
How do i actually do it? At the moment, i only know how to read the first line from the input file -
#include <stdio.h>
-
-
int main()
-
{
-
int input_character;
-
FILE *my_stream;
-
char my_filename[] = "command.txt";
-
// char my_string[100];
-
int bytes_read;
-
int nbytes = 100;
-
char *my_string;
-
-
my_stream = fopen (my_filename, "r");
-
-
my_string = (char *) malloc (nbytes + 1);
-
bytes_read = getline (&my_string, &nbytes,my_stream);
-
-
/* Close stream; skip error-checking for brevity of example */
-
fclose (my_stream);
-
-
printf ("%s", my_string);
-
-
return 0;
-
-
}
-
How do i actually do it? At the moment, i only know how to read the first line from the input file -
#include <stdio.h>
-
-
int main()
-
{
-
int input_character;
-
FILE *my_stream;
-
char my_filename[] = "command.txt";
-
// char my_string[100];
-
int bytes_read;
-
int nbytes = 100;
-
char *my_string;
-
-
my_stream = fopen (my_filename, "r");
-
-
my_string = (char *) malloc (nbytes + 1);
-
bytes_read = getline (&my_string, &nbytes,my_stream);
-
-
/* Close stream; skip error-checking for brevity of example */
-
fclose (my_stream);
-
-
printf ("%s", my_string);
-
-
return 0;
-
-
}
-
What if you put a loop around the getline statement? What if you didn't close the stream but instead did some functions then called getline again? There is nothing in the C language or steam class that says you have to read one line, then close the stream then do some functions with the data you read.
What if you put a loop around the getline statement? What if you didn't close the stream but instead did some functions then called getline again? There is nothing in the C language or steam class that says you have to read one line, then close the stream then do some functions with the data you read.
Thanks for the reply.
1) if my file contains multiple lines, how do i loop and print out each line until the end of file. I tried using read (bytes_read!=-1), but it gives me error.
2) I do not want to put a while loop bcos the function that will be running will invoke the readFile only when necessary..
Something like function A want to read 3 lines, then do it processing, then it want to read 4 lines.
Thks again
Thanks for the reply.
1) if my file contains multiple lines, how do i loop and print out each line until the end of file. I tried using read (bytes_read!=-1), but it gives me error.
2) I do not want to put a while loop bcos the function that will be running will invoke the readFile only when necessary..
Something like function A want to read 3 lines, then do it processing, then it want to read 4 lines.
Thks again
1) There must be a way to tell if the stream has more data in it to read or if it has reached the end of file. You will have to read up on the documentation of the stream code or getline to see what the end of file result is.
2) Then put the readFile inside an if statement and then if a certain condition is met then you can read the file again.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: fabrice |
last post by:
Hello,
I've got trouble reading a text file (event viewer dump) by using the
getline() function...
After 200 - 300 lines that are read correctly, it suddenly stops
reading the rest of the...
|
by: Michael |
last post by:
Hi,
I moved to c++ from c, and wanted to know what the best way to read data
from files is in c++. Any thoughts? fscanf() is possible but fairly painful!
Regards
Michael
|
by: SB |
last post by:
Hello. I have an input file which is laid out in the following manner...
Name
Day 1
am time 1
am time 2
appointment
pm time 1
pm time 2
appointment
Day 2
|
by: dbuser |
last post by:
Hi,
I need help on a problem, as described below. I am reading a file
"input.txt"which has data like this:
abc def gh izk lmnopq rst uvwxyz
I am using fstream object to read the file and...
|
by: googler |
last post by:
I'm trying to read from an input text file and print it out. I can do
this by reading each character, but I want to implement it in a more
efficient way. So I thought my program should read one...
|
by: paczkow |
last post by:
Dear Python Community,
I am an engineering and I am experiencing some trouble. Having output
data from other software I want to use it. To achieve this I decided to
use Python since this...
|
by: Anish G |
last post by:
Hi,
I have an issue with reading CSV files.
I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
|
by: Stephen.Schoenberger |
last post by:
Hello,
My C is a bit rusty (.NET programmer normally but need to do this in
C) and I need to read in a text file that is setup as a table. The
general form of the file is
00000000 USNIST00Z...
|
by: tushar.saxena |
last post by:
Hi,
I'm trying to read a file using the istearm class (I cant use ifstream
since the input might be a file or it might be stdin).
istream *input;
// Add checks for file name here, else use...
|
by: efrenba |
last post by:
Hi,
I came from delphi world and now I'm doing my first steps in C++. I'm
using C++builder because its ide is like delphi although I'm trying to
avoid the vcl.
I need to insert new features...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |