C语言如何将链表里的值写入文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言如何将链表里的值写入文件相关的知识,希望对你有一定的参考价值。

我用动态链表输入了很多信息,但是最后怎么才能把他们保存到文件里、以及下次要打开这个文件时、应该怎么打开、关于save 和load这两个函数要 怎么去写

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct datanode 
char name[24];
char phone[12];
// ......
struct datanode *next;
*pNode,*LinkList,Node;

LinkList getEmptyList() 
LinkList head = (pNode)malloc(sizeof(Node));
head->next = NULL;
return head;


void addNode(LinkList head, pNode pnode) 
pnode->next = head->next;
head->next = pnode;


void writeFile(LinkList head) 
FILE *outf;
pNode p = head->next;
if((outf = fopen("data.txt","wt")) == NULL) 
printf("不能打开数据文件。\\n");
exit(1);

while(p) 
fwrite(p,sizeof(Node),1,outf);
p = p->next;

fclose(outf);


int main() 
char name[24],phone[12];
// ......
pNode p;
LinkList head = getEmptyList();
printf("姓名 联系电话\\n");
while(scanf("%s%s",name,phone) == 2) 
p = (pNode)malloc(sizeof(Node));
strcpy(p->name,name);
strcpy(p->phone,phone);
addNode(head,p);
printf("姓名 联系电话(<Ctrl + Z> <ENTER> 结束)\\n");

writeFile(head);
return 0;

参考技术A 先定义一个全局的文件 FILE *fp;
读文件
if((fp=fopen("student.txt","r"))==NULL)/*打开文件*/
printf("文件打不开!\n");
exit(1);

while(!feof(fp))
p=(Student *)malloc(sizeof(Student));
fscanf(fp,"%s%s%s%d%d%d%d%d%d",
p->num,p->name,p->clas,&p->xf.jch,&p->xf.zhy,&p->xf.xx,&p->xf.rw,&p->xf.shy,&p->xf.zx);
p->next=sx->next;
p->prior=sx;
if(sx->next!=NULL)
p->next->prior=p;
sx->next=p;

fclose(fp);
写文件
if((fp=fopen("student.txt","w"))==NULL)

printf("文件打不开!\n");
exit(1);

while(p!=NULL)

fprintf(fp,"\n%s %s %s %d %d %d %d %d %d",
p->num,p->name,p->clas,p->xf.jch,p->xf.zhy,p->xf.xx,p->xf.rw,p->xf.shy,p->xf.zx);
p=p->next;

fclose(fp);追问

我用了两个动态链表~~分别保存了不同的结构体里面的信息~~是不是在写这两个函数的时候fprintf和fscanf都得写双份、还有while(!feof(fp))这里没看懂~~能否详细解释下

追答

一、你用了两个链表,好比两个数据库表,肯定是要写两个分别读取和保存对应文件,因为两个数据结构不同,无法重用代码;
二、fp是一个指针,我相信你已经看出来了,while(!feof(fp))是将指针左移并判断指向的地址是否有数据,如果有数据就继续操作,直到没有数据后退出循环,循环里的代码是先申请一个结构的空间,将数据读到这个这个结构变量里,然后再将这个变量插入到结构链表里,如此循环将所有数据读取插入链表就完成了文件的读取操作

本回答被提问者和网友采纳
参考技术B 看一下C语言的基本文件操作吧。。建议使用fwrite(),遍历链表的时候写入就OK。打开文件和这个相反,先看下基础吧。

如何将数据从闪亮的应用程序写入exce / csv文件?恰好我想将股票价格值的值写入excel / csv文件

我想将股票价格的值写入excel / csv文件。但是无法这样做。显示以下错误代码:如果没有活动的响应上下文,则不允许进行操作。 (您试图做只能从反应式表达式或观察器内部完成的操作。)。当我使用反应性数据(dataInput)时,错误消息显示为“无法强制将类'c(“ reactiveExpr”,“ reactive”))强制到data.frame

此代码附在此处。:

加载程序包----

library(shiny)
library(quantmod)
#edited the code.this can be run directly

# User interface ----
ui <- fluidPage(
  titlePanel("stockVis"),

  sidebarLayout(
    sidebarPanel(
      helpText("Select a stock to examine.

        Information will be collected from Yahoo finance."),
      textInput("symb", "Symbol", "SPY"),

      dateRangeInput("dates",
                     "Date range",
                     start = "2013-01-01",
                     end = as.character(Sys.Date())),

      br(),
      br(),

      checkboxInput("log", "Plot y axis on log scale",
                    value = FALSE)

      #checkboxInput("adjust",
      #"Adjust prices for inflation", value = FALSE)
    ),

    mainPanel(plotOutput("plot"), tableOutput("view")))



)


# Server logic
server <- function(input, output) 

  dataInput <- reactive(
    getSymbols(input$symb, src = "yahoo",
               from = input$dates[1],
               to = input$dates[2],
               auto.assign = FALSE)


  ) Blockquote
  output$plot <- renderPlot(

    chartSeries(dataInput(), theme = chartTheme("white"),
                type = "line", log.scale = input$log, TA = NULL)
  )

  output$view <- renderTable((dataInput() )
  , include.rownames = TRUE)
  #trying to export the data
  write.csv(dataInput(),row.names = TRUE)

`enter code here`

# Run the app
shinyApp(ui, server)
答案

在被动上下文中,一旦股票代码开始变化,它将尝试在运行Shiny应用程序后立即执行代码。要允许文件仅在用户准备就绪时才写入,请将“反应性”更改为“观察事件”。添加了“运行”按钮以使其工作。复制并粘贴下面的代码。

顺便说一下,'write.csv'命令中省略了'file =',该命令将csv文件滚动到控制台。

library(shiny)
library(quantmod)
#edited the code.this can be run directly

# User interface ----
ui <- fluidPage(
  titlePanel("stockVis"),

  sidebarLayout(
    sidebarPanel(
      helpText("Select a stock to examine.

        Information will be collected from Yahoo finance."),
      textInput("symb", "Symbol", "SPY"),

      dateRangeInput("dates",
                     "Date range",
                     start = "2013-01-01",
                     end = as.character(Sys.Date())),

      br(),
      br(),

      checkboxInput("log", "Plot y axis on log scale",
                    value = FALSE),

      #checkboxInput("adjust",
      #"Adjust prices for inflation", value = FALSE),
      actionButton(inputId = "run",label="Run"),
    ),

    mainPanel(plotOutput("plot"), tableOutput("view")))

)

# Server logic
server <- function(input, output) 

  dataInput <- function() 
    getSymbols(input$symb, src = "yahoo",
               from = input$dates[1],
               to = input$dates[2],
               auto.assign = FALSE)
  

  observeEvent (input$run, 

   output$plot <- renderPlot(

    chartSeries(dataInput(), theme = chartTheme("white"),
                type = "line", log.scale = input$log, TA = NULL)
    )

    output$view <- renderTable((dataInput() )
    , include.rownames = TRUE)
    #trying to export the data
    write.csv(dataInput(),row.names = TRUE)

  )

# Run the app

shinyApp(ui, server)

以上是关于C语言如何将链表里的值写入文件的主要内容,如果未能解决你的问题,请参考以下文章

MySQL数据写入后显示NULL

C语言如何以插入方式写入文件

C:链表并写入文件

PHP 中如何在同一个文件中写入而不覆盖以前写的内容

C语言如何写入文本文件

在C语言中,fopen一个文件 如何能够在写入新的数据覆盖原文件中指定长度的内容