Hi there,
I know that Java normally has got .startswith to check if it from the start
but for C i use strcmp is it correct?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct vertex {
int vid;
float x;
float y;
float z;
float nx;
float ny;
float nz;
};
struct face{
int fid;
float v1;
float v2;
float v3;
};
int main(void) {
char filename1[80];
FILE *fp1;
printf("Enter the input file name:");
gets(filename1);
fp1=fopen(filename1,"r");
printf("EOF is %d",EOF);
char string[100];
if (fp1!=NULL)
{
char *ch;
while( fgets(string,100,fp1) != NULL)
{
if(strcmp(string,"Vertex") == 1){
printf("\n");
do{
if(strcmp(string,"Vertex") == 1){
struct vertex m;
int c = 0;
/* printf("%s",string); */
ch = strtok(string, " ");
while(ch != NULL){
char *type = "";
if(c == 0){
type = ch;
}else if(c == 1){
m.vid = atof(ch);
}else if(c == 2){
m.x = atof(ch);
}else if(c == 3){
m.y = atof(ch);
}else if(c == 4){
m.z = atof(ch);
}
float len = sqrt(pow(m.x,2)+pow(m.y,2)+pow(m.z,2));
m.nx = m.x/len;
m.ny = m.y/len;
m.nz = m.z/len;
c++;
ch = strtok(NULL, " ");
}
printf("%i %f %f %f =normal(%f %f %f)\n",m.vid,m.x,m.y,m.z,m.nx,m.ny,m.nz);
}
else if(strcmp(string,"Face") == 1){
struct face m;
int c = 0;
/* printf("%s",string); */
ch = strtok(string, " ");
while(ch != NULL){
char *type = "";
if(c == 0){
type = ch;
}else if(c == 1){
m.fid = atof(ch);
}else if(c == 2){
m.v1 = atof(ch);
}else if(c == 3){
m.v2 = atof(ch);
}else if(c == 4){
m.v3 = atof(ch);
}
c++;
ch = strtok(NULL, " ");
}
printf("%i %f %f %f\n",m.fid,m.v1,m.v2,m.v3);
}
}while( fgets(string,100,fp1) != NULL);
}
}
}
fclose (fp1);
return(0);
}
Hi there,
I am trying to learn c programming,it was mentioned that C is backward compatiable with java right?
So far I have explored options and found out that visual C++ 2010 is hard to use compared to borland commandline tools.But I was ask to used visual studio because I was developing an assignment in openGL,there are some linker errors...like no incremental..cannot find .exe files etcs...takes me weeks to figure this out..cannot import glut.h etc
What is the equivalent of ArrayList or array implementation in C?
In java there is strtokenizer,but for c it is like strtok which is a pointer.
even declaring a string in c needs to use like char str[] or char str*
I need to cast a string to float needs atof,but for just I just use Integer.parseInt():
event printing a line needs use printf()..where in python just print() or java just System.out.println()
I am trying to write a parser btw.
char arr[5];
char *arr = malloc(sizeof(char) * 5);
Vertex 1 0.120846 0.0669721 -0.0628787
Vertex 2 0.312241 0.00198185 0.0681098
Vertex 3 0.140731 0.444648 -0.0623968
Face 1 16563 41411 41412
Face 2 16567 16597 22256
Face 3 22245 16567 22256
for my parser I am trying to do IO of a file in C.
the file I am trying to read into struct is:
say I don't know how many lines to read,i want to know know to insert into that dynamic array you mention.
Vertex 1 0.120846 0.0669721 -0.0628787 Vertex 2 0.312241 0.00198185 0.0681098 Vertex 3 0.140731 0.444648 -0.0623968 Face 1 16563 41411 41412 Face 2 16567 16597 22256 Face 3 22245 16567 22256
FILE *FP;
struct _point {
float x;
float y;
float z;
};
struct _record {
char nodename[20];
int nodeidx;
struct _point pt;
};
while (not end of file) {
struct _record r;
int rv;
rv = fscanf(FP, "%s%d%f%f%f", r.nodename, &r.nodeidx, &r.pt.x, &r.pt.y, &r.pt.z);
if (rv != 5) {
print error
}
}
Okay, consuming input have quite a number of ways, you will need to think about how to consume input in a more robust way in the future.
I will give you something simple for you to consider
From the perspective of C, a file is just a series of bytes, there is no notion of multiple lines, that's what you perceive when the display print in multiple lines due to the CRLF limiters.
One way to look at your input is as follows
Code:Vertex 1 0.120846 0.0669721 -0.0628787 Vertex 2 0.312241 0.00198185 0.0681098 Vertex 3 0.140731 0.444648 -0.0623968 Face 1 16563 41411 41412 Face 2 16567 16597 22256 Face 3 22245 16567 22256
All characters under this group are consider whitespaces '\t', ' ', '\r', '\n', '\f', '\v'
So actually your parsing of this file can be recognise in the following manner
[WORD][WHITESPACES][NUMBER][WHITESPACES][FLOAT][WHITESPACES][FLOAT][WHITESPACES][FLOAT][WHITESPACES]
You will keep on consume in the above form until the end-of-file is encountered. The solution proposed is naive and doesn't takecare of incorrect input or incomplete input, but it will work in your case.
The key function is fscanf, either do a "man fscanf" in a unix system, or google for the online manual.
Below is a fragment codes to the idea
Code:FILE *FP; struct _point { float x; float y; float z; }; struct _record { char nodename[20]; int nodeidx; struct _point pt; }; while (not end of file) { struct _record r; int rv; rv = fscanf(FP, "%s%d%f%f%f", r.nodename, &r.nodeidx, &r.pt.x, &r.pt.y, &r.pt.z); if (rv != 5) { print error } }
Now obviously it didn't take care of how to open file, and dynamically allocate memory, that will be an exercise for you.
The simplest dynamic data structure to cater to your needs is a Linked List, go do some research on it.
Here are some reading materials for you
Linked Lists in C Tutorial - Cprogramming.com
Linked list - Wikipedia, the free encyclopedia
i see that you are using rv!=5,which means count 5,this is much better than i used the what atof if else else etcs..it is similar to my teachers code with fscanf.
//can save the vertex into the vertex array,
but from what i know array is only like this:
#define MTHS 12 /* define a constant */
int days[MTHS]={31,28,31,30,31,30,31,31,30,31,30,31};
the C course I attended before never teach me malloc,
it goes up to the C processors only
void *p = malloc(1000);
int *i = p;
char *c = p;
long *f = p;
//can save the vertex into the vertex array,
but from what i know array is only like this:
#define MTHS 12 /* define a constant */
int days[MTHS]={31,28,31,30,31,30,31,31,30,31,30,31};
the C course I attended before never teach me malloc,
it goes up to the C processors only
but there is a problem,
Face 1 16563 41411 41412
these face are references to the vertex,
I still needed to store in linked list and iterate through after the fully iterate stored into the linkedlist
i feel like doing this but i can't,i got three different files vertex line is of arbitary 10k to 20k,
I recalled when I do in java,for linked list,i have to write a search function to tranverse through the linkedlist and get the vid(vertex id)
I also recalled in java,there is a hashmap(1,x),hashmap(2,x) as well,i am looking for simpler alternatives.
public static Customer findCustomer(Customer c) {
Customer foundCustomer = null;
// Searches for duplicated object based on E's compareTo attribute and returns if found.
Node<Customer> cur = customerList.head;
while (cur != null) {
// Get customer object if found
if (cur.item.getAccountId() == c.getAccountId()) {
foundCustomer = cur.item;
}
// Go to next Customer object
cur = cur.next;
}
return foundCustomer;
}
I looked back 3 years ago,
there is this code,
I remember I got difficulty implementing it in one of my assignment and failed it
I was thinking how I can implement a compareTo in it.
#include <stdio.h>
#include <stdlib.h>
typedef struct _NODE {
void *data;
struct _NODE *next;
} NODE;
typedef NODE* pNODE;
typedef struct _LIST {
pNODE head;
pNODE tail;
int size;
} LIST;
typedef LIST* pLIST;
typedef void* (*SEARCHFUNC)(pNODE node, void *key);
typedef void (*DISPLAYFUNC)(pNODE node);
pLIST llist_create() {
pLIST p = malloc(sizeof(LIST));
p->head = NULL;
p->tail = NULL;
p->size = 0;
return p;
}
pLIST llist_free(pLIST* pplist) {
free(*pplist);
pplist = NULL;
// is this good enough to free a list ?
}
pLIST llist_add(pLIST l, void *data) {
if (l == NULL || data == NULL) return NULL;
if (l->head == NULL) {
l->head = l->tail = malloc(sizeof(NODE));
}
else {
l->tail->next = malloc(sizeof(NODE));
l->tail = l->tail->next;
}
l->tail->data = data;
l->tail->next = NULL;
l->size++;
return l;
}
int llist_size(pLIST l) {
return l->size;
}
void* llist_find(pLIST l, void *key, SEARCHFUNC f) {
pNODE n;
void *data = NULL;
if (l == NULL || key == NULL || f == NULL) return NULL;
n = l->head;
while (n != NULL) {
data = f(n, key);
if (data != NULL) break;
n = n->next;
}
return data;
}
void llist_display(pLIST l, DISPLAYFUNC f) {
pNODE n;
if (l == NULL || f == NULL) return;
n = l->head;
while (n != NULL) {
f(n);
n = n->next;
}
}
void display_mynode(pNODE n) {
if (n != NULL) {
printf("%d", *((int*)n->data));
if (n->next != NULL)
printf(", ");
else
printf(".\n");
}
}
int main(int argv, char **argc) {
int data1 = 1;
int data2 = 10;
int data3 = 100;
pLIST mylist = llist_create();
llist_add(mylist, &data1);
llist_add(mylist, &data2);
llist_add(mylist, &data3);
llist_display(mylist, display_mynode);
return 0;
}
thanks,
i am studying for quiz searching for answer on this now:
what is (GL_modelView)
what is difference between GL_Triangle_strip and GL_Triangle
why the need to loadIdentitymatrix first
what is glmatrixmode
#include <stdio.h>
#include <stdlib.h>
typedef struct vertex {
int vid;
float x;
float y;
float z;
float nx;
float ny;
float nz;
}vertex;
int main(void) {
struct vertex* pArray = NULL; /* My array */
int nArraySize = 0;
nArraySize = 1;
pArray = (struct vertex*)realloc(pArray, sizeof(struct vertex) * nArraySize);
printf("%s",pArray[0]);//does work
printf("%s",pArray[1]);//cause program to crash
printf("%s",pArray[2]);//null pointer
return(0);
}