ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
So I've been working on this code for a bit but I cannot seem to get it to function properly. I think there is something wrong with my variable types due to the compiler error. The purpose of this code is to read in a .xyz coordinate file and assign varialbes in three arrays whose indicies correspond to the atom number.
Code:
3$ gcc LJ_CUDA_D1.c -o LJ_CUDA_D1.o
LJ_CUDA_D1.c:48:6: warning: conflicting types for ‘f2input’ [enabled by default]
LJ_CUDA_D1.c:24:4: note: previous implicit declaration of ‘f2input’ was here
Here is my code:
Code:
#include<stdio.h>
#include<stdlib.h>
main(int argc, char *argv[]){
int i;
FILE *fp;
char *inputfile = "-i";
//printf("inputfile switch is %s\n",inputfile);
for(i=1;i<argc;i++){
printf("argv[%d] = %s\n", i, argv[i]);
//if(strcmp(argv[i],inputfile) != 1){
if(*argv[i] == *inputfile){
++i;
if((fp = fopen(argv[i], "r")) == NULL){
printf("Error in opening file %s\n", *argv);
return 1;
}
else {
//fp = fopen(argv[i], "r"); // No sense repeating the same function.
printf("%s is open\n",argv[i]);
//filecopy(fp, stdout); // debugging line
f2input(fp);
//printf("Atom one coodinates x %f, y %f, and z %f\n",atomx[1],atomy[1],atomz[1]);
fclose(fp);
return 1;
}
}
}
return 0;
}
void finput(FILE *ifp)
// use atof() here!!!
{
char str[128], c;
int row = 1;
while(!feof(ifp))
{
if(row == 1){
if(fgets(str, '\t', ifp)){
printf("%s", str);
++row;
}
}
}
}
void f2input(FILE *ifp)
{
int N;
char c;
N = 0;
while((c = getc(ifp)) != EOF){
if(c == '\n')
++N; //Indicates the numnber of atoms based on # of lines in the file.
}
int atom = 0;
float *atomx = malloc(N*sizeof(float));
float *atomy = malloc(N*sizeof(float));
float *atomz = malloc(N*sizeof(float));
while(fscanf(ifp, "%f %f %f", &atomx[atom], &atomy[atom], &atomz[atom]) != EOF){
fscanf(ifp, "%f %f %f", &atomx[atom], &atomy[atom], &atomz[atom]);
++atom;
}
printf("Atom one coodinates x %f, y %f, and z %f\n",atomx[1],atomy[1],atomz[1]);
}
/* void fileinput(FILE *ifp) */
/* { */
/* int N, column, atom; */
/* char c; */
/* column == 1; */
/* N = 0; */
/* atom = 0; */
/* while((c = getc(ifp)) != EOF){ */
/* if(c == '\n') */
/* ++N; //Indicates the numnber of atoms based on # of lines in the file. */
/* } */
/* double *atomx = malloc(N*sizeof(double)); */
/* double *atomy = malloc(N*sizeof(double)); */
/* double *atomz = malloc(N*sizeof(double)); */
/* while((c = getc(ifp)) != EOF){ */
/* if(column == 1){ */
/* //try atof here */
/* double c = atomx[atom]; */
/* if(c == '\t') */
/* ++column; */
/* } if(column == 2){ */
/* double c = atomy[atom]; */
/* if(c == '\t') */
/* ++column; */
/* } */
/* if(column == 3){ */
/* double c = atomz[atom]; */
/* if(c == '\n'){ */
/* column = 1; */
/* ++atom; */
/* } */
/* } */
/* } */
/* printf("Atom one coodinates x %f, y %f, and z %f\n",atomx[1],atomy[1],atomz[1]); */
/* } */
/* filecopy: copy file ifp to file ofp. For debuggage */
void filecopy(FILE *ifp,FILE *ofp)
{
int c;
while((c = getc(ifp)) != EOF)
putc(c, ofp);
}
I have been experimenting with different methods but so far none have worked. Any help would be appreciated. The output of the current code is:
Code:
$ ./LJ_CUDA_D1.o -i test.txt
argv[1] = -i
test.txt is open
Atom one coodinates x 0.000000, y 0.000000, and z 0.000000
That took care of the error. However fscanf() still did not assign any values to the atomx, atomy, atomz arrays. Thus, I still get the same output as before. Any thoughts?
EOF is used with getchar() and related functions, not fscanf. fscanf returns number of read and assigned values. You want to compare to 3 (since this is how many variables you have).
PS. It would be helpful if you provided a nicely formatted file. By nicely formatted I mean at least the two: (i) no commented code – if it's not executed, just don't include it, and (ii) use consistent indentation. (i) also goes with the fact that your code should be minimal that shows the issue you're encountering.
My goal in the fileinput() function is to open and read in to the arrays the tab deliminated values in the input file. However with the diagnostic printf statement the output is the same as before no values are assigned:
Code:
$ ./input.o -i ./test.txt
Atom one coodinates x 0.000000, y 0.000000, and z 0.000000
I'm still not sure why this is not working. I'd really appreciate any help.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.