473,396 Members | 1,892 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,396 software developers and data experts.

getline and C

Hello,

I am new at computer programming and I am having trouble using getline() in C.
I use an input file that contains 12 variables on every line.
From the printf statements below, I notice that scanf, although risky, does the job correctly.

However, getline skips the first line from the input file and evaluates the last line twice.
Do you have any idea what I am doing wrong?

Thank you for your comments!
Regards,
Tom

#include <stdio.h>

float aa,ab,ac,ba,bb,bc,ca,cb,cc,da,db,dc;
struct point {
float x;
float y;
float z;
};
char *memal;
int nbytes = 1000;

main()
{

while (getline(&memal, &nbytes, stdin) != -1)
{
scanf("%6f %7f %7f %7f %7f %7f %7f %7f %7f %7f %7f %7f", &ca, &cb, &cc, &aa, &ab, &ac, &da, &db, &dc, &ba, &bb, &bc);

struct point ptx0 = {aa, ab, ac};
struct point ptx1 = {ba, bb, bc};
struct point ptx2 = {ca, cb, cc};
struct point ptx3 = {da, db, dc};

printf("%f\n", ptx2.x);
printf("%f\n", ptx2.y);
printf("%f\n", ptx2.z);
printf("%f\n", ptx1.z);

etc.
Mar 21 '07 #1
2 2360
horace1
1,510 Expert 1GB
the getline() will skip every other line. If all lines contain 12 values just check the conversion works OK, e.g.
Expand|Select|Wrap|Line Numbers
  1. while (scanf("%6f %7f %7f %7f %7f %7f %7f %7f %7f %7f %7f %7f", 
  2.     &ca, &cb, &cc, &aa, &ab, &ac, &da, &db, &dc, &ba, &bb, &bc)==12)
  3. {
  4.   // 12 values read ..
  5. ...
  6. }
  7. // read failed
  8.  
Mar 21 '07 #2
Thank you for your comment!

I did what you indicated, replacing the getline function using scanf as a test.
Now it sometimes skips lines in the middle of the input file as well, even though they all contain 12 variables. However, the last line is only evaluated once.
The input file is actually the result of an other program that puts these 12 variables on a line, followed in the end by \n, so I guess there should be no difference between these lines.

Here's part of the program where the input file is being created.

for (i=0; i< imax; i++)
{
if (num == (2+i*7)){
struct bead bdeth = {xx, yy, zz};
printf("%6.3f %6.3f %6.3f ", bdeth.x, bdeth.y, bdeth.z);
}
else if (num == (3+i*7)){
struct bead bdori = {xx, yy, zz};
printf("%6.3f %6.3f %6.3f ", bdori.x, bdori.y, bdori.z);
}
else if (num == (4+i*7)){
struct bead bdpro = {xx, yy, zz};
printf("%6.3f %6.3f %6.3f ", bdpro.x, bdpro.y, bdpro.z);
}
else if (num == (7+i*7)){
struct bead bdmet = {xx, yy, zz};
printf("%6.3f %6.3f %6.3f\n", bdmet.x, bdmet.y, bdmet.z);
}
Mar 21 '07 #3

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

Similar topics

2
by: Vikram | last post by:
Hi, I don't remember if it happened previously, but nowadays I'm having problem with using cin.getline function and cin>> function simultaneously. I have Visual Studio 6. If I use cin.getline...
5
by: vknid | last post by:
Hello, I have a question. Its probably a very newbish question so please be nice hehe. =D I have been reading through C++ Programming Fundamentals, and have come a crossed an example program...
1
by: ma740988 | last post by:
Consider: ifstrem MyFile("extractMe.txt"); string Str; getline(MyFile, Str); getline above extracts the contents of MyFile and place into the string object. Deduced using FROM/TO logic I...
10
by: Skywise | last post by:
I keep getting the following error upon compiling: c:\c++ files\programs\stellardebug\unitcode.h(677) : error C2664: 'class istream &__thiscall istream::getline(char *,int,char)' : cannot convert...
14
by: KL | last post by:
I am so lost. I am in a college course for C++, and first off let me state I am not asking for anyone to do my assignment, just clarification on what I seem to not be able to comprehend. I have a...
18
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
2
by: jalkadir | last post by:
I am trying to get character string from the user, to do that I use getline(char_type*, streamsize), but I get a segmentation fault??!! Can anyone give me a hand, what am I doing wrong? --snip...
5
by: allspamgoeshere3 | last post by:
Hi! I'm having trouble reading text lines from a file describing the levels for a simple game I'm creating. Inside the function that reads the content of the file there is a loop that basically...
33
by: Chen shuSheng | last post by:
I have a code: --------------------------- #include <iostream.h> #include <stdlib.h> int main() { int max=15; char line; getline(line,max); system("PAUSE");
3
by: JackC | last post by:
Hi, How do i use stringstreams getline function to extract lines from an existing string? Say i have: string strlist = "line1\r\nLine2\r\nLine3\r\n"; I want to extract each line out into a...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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 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.